我是python新手,有以下问题。我有一个嵌套字典,形式为
dict = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
,并希望找到所有具有相同值的键。输出应与此类似。
1 : [a,b]
2 : [a,c]
..
在此先感谢您的帮助!
答案 0 :(得分:4)
dict = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
output = {}
for key, value in dict.items():
for v in value:
if v in output.keys():
output[v].append(key)
else:
output[v] = [ key ]
print(output)
输出将是
{'2': ['a', 'c'], '1': ['a', 'b'], '5': ['b'], '3': ['c']}
答案 1 :(得分:1)
您可以使用defaultdict
轻松构建输出(如果您希望按排序顺序对键进行排序):
from collections import defaultdict
d = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
out = defaultdict(list)
for key, values in d.items():
for value in values:
out[value].append(key)
# for a sorted output (dicts are ordered since Python 3.7):
sorted_out = dict((k, out[k]) for k in sorted(out))
print(sorted_out)
#{'1': ['a', 'b'], '2': ['a', 'c'], '3': ['c'], '5': ['b']}
答案 2 :(得分:1)
在我们寻求解决方案之前,莱姆告诉您一些事情。您所拥有的不是嵌套字典,而是字典中的设置。
一些python术语可以清除这一点:
数组:[1,2]
数组用大括号括起来,并用逗号分隔。
字典:{“ a”:1,“ b”:2}
字典用大括号括起来,并用逗号分隔“ key”:value对。这里,“ a”和“ b”是键,而1和2分别是它们的值。
设置:{1,2}
集合用大括号括起来,并用逗号分隔。
dict = {'a':{'1','2'},'b':{'5','1'},'c':{'3','2'}}
在这里, {'1','2'} 是字典中的键为'a'的集合。因此,您所拥有的实际上是在词典中设置的,而不是嵌套词典中的。
进入解决方案,集合不是可迭代的,这意味着您不能一一遍解它们。因此,您必须将它们转换为列表,然后对其进行迭代。
# Initialize the dictionary to be processed
data = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
# Create dictionary to store solution
sol = {} # dictionary to store element as a key & sets containing that element as an array
# Eg., sol = { "1" : [ "a" , "b" ] }
# This shows that the value 1 is present in the sets contained in keys a & b.
# Record all elements & list every set containing those elements
for key in data. keys (): # iterate all keys in the dictionary
l = list ( data [ key ] ) # convert set to list
for elem in l: # iterate every element in the list
if elem in sol. keys (): # check if elem already exists in solution as a key
sol [ elem ]. append ( key ) # record that key contains elem
else:
sol [ elem ] = [ key ] # create a new list with elem as key & store that key contains elem
# At this time, sol would be
# {
# "1" : [ "a" , "b" ] ,
# "2" : [ "a" , "C" ] ,
# "3" : [ "c" ] ,
# "5" : [ "b" ]
# }
# Since, you want only the ones that are present in more than 1 sets, let's remove them
for key in sol : # iterate all keys in sol
if sol [ key ]. length < 2 : # Only keys in at least 2 sets will be retained
del sol [ key ] # remove the unrequired element
# Now, you have your required output in sol
print ( sol )
# Prints:
# {
# "1" : [ "a" , "b" ] ,
# "2" : [ "a" , "c" ]
# }
希望对您有帮助...
答案 3 :(得分:0)
您可以反转dict中的键值,创建一个值-键dict,如果您只想要重复的值(find all the keys that have the same values
),则可以filter
:
from collections import defaultdict
def get_duplicates(dict1):
dict2 = defaultdict(list)
for k, v in dict1.items():
for c in v:
dict2[c].append(k)
# if you want to all values, just return dict2
# return dict2
return dict(filter(lambda x: len(x[1]) > 1, dict2.items()))
输出:
{'1': ['a', 'b'], '2': ['a', 'c']}
答案 4 :(得分:0)
使用defaultdict
中的collections
可以轻松完成此操作,
>>> d = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
>>> from collections import defaultdict
>>> dd = defaultdict(list)
>>> for key,vals in d.items():
... for val in vals:
... dd[val].append(key)
...
>>>>>> dict(dd)
{'1': ['a', 'b'], '3': ['c'], '2': ['a', 'c'], '5': ['b']}
答案 5 :(得分:0)
这可以通过两个内部for循环轻松实现:
dict = {'a': {'1','2'}, 'b':{'5','1'}, 'c':{'3','2'}}
out = {}
for key in dict:
for value in dict[key]:
if value not in out:
out[value]= [key]
else:
out[value]+= [key]
print out # {'1': ['a', 'b'], '3': ['c'], '2': ['a', 'c'], '5': ['b']}