python字典相同的key
Ⅰ python 字典提取出value相同的key
hi,你好。下面是找到value相同的key的python代码。
new_dict = {}
for k, v in some_dict.items():
new_dict.setdefault(v, []).append(k)
Ⅱ python 如何将列表转为字典,key值相同则合并value
a='齐建国 齐建国 齐二石 黄鲁成 黄津孚'.split()
b=[13,33,38,26,31]
d={}
for x in range(len(a)):
if a[x] in d:
d[a[x]]+=[b[x]]
else:
d[a[x]]=[b[x]]
print(d)
Ⅲ 如何判断python两个字典里面相同key对应的值是否相等
for key in a:
if key in c:
if c[key] == a[key]:
print('key %s 相等' % key)
else:
print('key %s 不相等' % key)
else:
print('c中无key %s' % key)
Ⅳ python如何实现列表嵌套字典,字典内相同key去重,字典内另外一个key的value相加
按照你的要求编写的字典内相同key合并的Python程序如下
l=[{'a':1,'b':'haha'},{'a':3,'b':'haha'},{'a':2,'b':'xiaoming'}]
result=[]
temp=[]
for i in range(len(l)):
flag=False
suma=l[i]['a']
b=l[i]['b']
for j in range(i+1,len(l)):
if l[i]['b']==l[j]['b'] and (j not in temp):
flag=True
temp.append(i)
temp.append(j)
suma=suma+l[j]['a']
if i not in temp or flag==True:
result.append({'a':suma,'b':b})
print(result)
源代码(注意源代码的缩进)
Ⅳ python 字典可以有相同的key吗
不可以,因为key是索引,给一个已经存在的key赋值即会改变它的值。
a={'a':1,'b':2}
#{'a':1,'b':2}
a['c']=3
#{'a':1,'b':2,'c':3}
a['a']=4
#{'a':4,'b':2,'c':3}
Ⅵ python列表如何转字典 取相同key值把values相加
mobile=[['apple','ios','100','10'],['pear','android','200','20'],['apple','ios','500','50'],['pear','android','600','60']]
mobiledict={}
foreleminmobile:
key=(elem[0],elem[1])
ifkeyinmobiledict:
mobiledict[key][0]+=int(elem[2])
mobiledict[key][1]+=int(elem[3])
else:
mobiledict[key]=[int(elem[2]),int(elem[3])]
print(mobiledict)
Ⅶ python字典 相同KEY整成一个列表
由于些软件无法正常输出所需格式样式,只能截图给你。
图1为代码,图2为效果。
Ⅷ Python 如果两个字典key值相同,如何提取对应values组成新的字典
是这样吗?
Ⅸ Python两个字典key相同的值组成新字典
按照你的要求编写的两个字典key相同的值组成新字典的Python程序如下
dic1={'福州龙湖': ['1293521.23', '620624'], '赣州龙湖': ['3050.51']}
dic2={'福州龙湖': ['12345', '67890'], '赣州龙湖': ['123','456']}
dic3={}
for key in dic1:
if dic2.get(key):
dic3[key]=[dic1[key],dic2[key]]
else:
dic3[key]=dic1[key]
for key in dic2:
if dic1.get(key):
pass
else:
dic3[key]=dic2[key]
print(dic3)
源代码(注意源代码的缩进)
运行结果
{'福州龙湖': [['1293521.23', '620624'], ['12345', '67890']], '赣州龙湖': [['3050.51'], ['123', '456']]}