标签:print 字典 通过 val code pre int for 查询
可迭代的键:字符串,列表,元组,字典等;value可以嵌套
dic = dict.fromkeys({1,2,3},([],[])) print(dic) dic[1][0].append(‘aike‘) print(dic) dic[2][0].append(‘艾克‘) print(dic) dic[2][1].append(‘183‘) print(dic)
输出:
{1: ([], []), 2: ([], []), 3: ([], [])} {1: ([‘aike‘], []), 2: ([‘aike‘], []), 3: ([‘aike‘], [])} {1: ([‘aike‘, ‘艾克‘], []), 2: ([‘aike‘, ‘艾克‘], []), 3: ([‘aike‘, ‘艾克‘], [])} {1: ([‘aike‘, ‘艾克‘], [‘183‘]), 2: ([‘aike‘, ‘艾克‘], [‘183‘]), 3: ([‘aike‘, ‘艾克‘], [‘183‘])}
注:dic为字典,字典查询只有 for 、get和通过键查询,所以
dic[1][0]
dic[2][0]
当中的1和2对应的是dic的键值,例如:
dic = dict.fromkeys({‘a‘,‘b‘,‘c‘},([],[])) print(dic) dic[‘a‘][0].append(‘aike‘) print(dic) dic[‘b‘][0].append(‘艾克‘) print(dic) dic[‘c‘][1].append(‘183‘) print(dic)
输出
{‘a‘: ([], []), ‘c‘: ([], []), ‘b‘: ([], [])}
{‘a‘: ([‘aike‘], []), ‘c‘: ([‘aike‘], []), ‘b‘: ([‘aike‘], [])}
{‘a‘: ([‘aike‘, ‘艾克‘], []), ‘c‘: ([‘aike‘, ‘艾克‘], []), ‘b‘: ([‘aike‘, ‘艾克‘], [])}
{‘a‘: ([‘aike‘, ‘艾克‘], [‘183‘]), ‘c‘: ([‘aike‘, ‘艾克‘], [‘183‘]), ‘b‘: ([‘aike‘, ‘艾克‘], [‘183‘])}
fromkeys:创建一个新的字典,使用可迭代的键和设置为value的值
标签:print 字典 通过 val code pre int for 查询
原文地址:https://www.cnblogs.com/aizhinong/p/11335419.html