zoukankan      html  css  js  c++  java
  • 计数,dic的创建方式,求九九乘法表

    s1='char,python,nihao,ni,ni,python'
    s=s1.split(',')
    print(s1)
    s2=list()
    for i in s:
    if i not in s2:
    s2.append(i)
    s2.append(1)
    else:
    if i in s2:
    s2[s2.index(i)+1]= str(int(s2[s2.index(i)+1])+1)
    print(s2)
    i=0
    while i<len(s2):
    print(s2[i],s2[i+1],sep='---')
    i+=2




     dic = {}
    # 第一种:
    # 创建空字典
    # dic = {}
    # type(dic)# <type 'dict'>

    # 2:通过赋值创建。
    # dic = {'spam':1,'egg':2, 'bar':3}

    # 3:通过dict函数和关键字参数
    # dict()
    # dic = dict(spam = 1, egg = 2, bar =3)
    # print(dic)
    # {'spam':1,'egg':2,'bar':3}
    # *args,**kwargs
    # **kwargs
    # def index_01(gender = '男')

    # 默认参数。
    # def print(self, *args, sep=' ', end=' ', file=None):
    # 关键字参数。
    # print(end=' ')

    # 4. 通过列表转字典。
    # l = [('spam', 1), ('egg', 2), ('bar', 3)]
    # dic = dict(l)
    # print(dic)
    # {'spam': 1, 'egg': 2, 'bar': 3}

    # 5:通过dict函数和zip函数获取
    # dic = dict(zip('abc', [1, 2, 3]))
    # zip

    # dic = dict(zip('abc', [1, 2, 3]))
    # print(dic)

    # 6:通过字典推导式创建。
    # dic = {i:2*i for i in range(3)}

    # dic = { str(i) : i * 2 for i in range(9) }
    # 可以删选,加if
    # '0':0,'1':2....

    # print(dic)
    # k:v
    # [ i*2 for i in range(9)]

    # { str(i) :i*2 for i in range(9) }

    # 7:通过dict.fromkeys()创建
    # 通常用来初始化字典, 设置value的默认值

    # dic = dict.fromkeys('nihao',3)
    # # {'n': 3, 'i': 3, 'h': 3, 'a': 3, 'o': 3}
    # print(dic)

    # {'k':3,'i':3,'h':3,'a':3}

    # dict.
    # class dict
    # # 装饰器。语法糖
    # @classmethod, @overload
    # def fromkeys(cls, __iterable: Iterable[_T])
    # -> Dict[_T, Any]
    # Possible types: • (cls: Type[dict], __iterable: Iterable[_T]) 
    # : _S) -> Dict[_T, _S]
    #
    # Create a new dictionary
    # with keys from iterable
    # and values set to value.
    # key : value
    # nihao

    # 8:其他方式
    l = ['x', 1, 'y', 2, 'z', 3]
    # l[::2]
    # l[1::2]
    # dict(zip(['x','y','z'],[1,2,3]))
    dic = dict(zip(l[::2], l[1::2]))
    print(dic)
     
    for i in range(1,10):
    # for j in range(1,i+1):
    # print(f'{j} * {i} = {j*i} ',end=' ')
    # print()
     
  • 相关阅读:
    应用程序与数据库结合使用的三种方式
    mysql内置功能—存储过程
    mysql内置功能—事务
    mysql内置功能—触发器
    mysql内置功能—视图
    pymysql模块
    SQL逻辑查询语句执行顺序
    多表查询
    单表查询
    数据的增删改查
  • 原文地址:https://www.cnblogs.com/lfdyyy/p/13301002.html
Copyright © 2011-2022 走看看