mshd.net
当前位置:首页 >> python 字典一个kEy怎么对应多个vAluE >>

python 字典一个kEy怎么对应多个vAluE

a = {1:'a',3:'b',5:'c'} b,c = a.keys() , a.values()

dic = {"1":"a,b,c", "2":"d"} for k, v in dic.items(): valus = v.split(",") for value in values: print k, value

before = { "key1": 5, "key2": 6, "key3": 4, "key4": 3,}# 排序after = dict(sorted(before.items(), key=lambda e: e[1]))print(after)# 取出前几个, 也可以在sorted返回的list中取前几个cnt = 0 for key, value in after.items(): cnt += 1...

dict ={'a':2,'b':3,'c':1}1.print(sorted(dict,key=lambda x:dict[x])[-1])2.print filter(lambda x:max(dict.values())==dict[x],dict)[0]

dic = {"1":"a,b,c", "2":"d"} for k, v in dic.items(): valus = v.split(",") for value in values: print k, value

data = {1: 10002, 2: 10002, 3: 10002, 4: 10002, 5: 10002, 6: 10007, 7: 10007, 8: 10007, 9: 10007, 10: 10007, 11: 10007, 12: 10007, 13: 10007}items = list(data.items())items.sort(key=lambda i: -i[0])tmp = {val: key for key, val ...

dict的value是可以重复的,如果你确定你的value不会重复,那效率最高的办法是用一正一反两个dict。

字典的key值是不可以重复的,如果重复默认取最后一个value值。 如果做 print dict_data 结果是:{'b':'2', 'a':'ddd'}。 key不能重复,但是字典的value值可以是任意类型。 所以可以给‘a’对应的value存成list dict_data = {'a':['1','3','ddd'], ...

对应值:value = dictname[key]。字典是Python语言中唯一的映射类型。映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关系,通常被认为是可变的哈希表。 典对象是可变的,它是一个容器类型,能存储任意个数的Python对象...

#加一层判断 dict1 = dict()for i,j in zip(g[::2],g[1::2]): if i not in dict1.keys(): dict1[i] = j

网站首页 | 网站地图
All rights reserved Powered by www.mshd.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com