Python 3.x 字典python 3中值的括号

Python 3.x 字典python 3中值的括号,python-3.x,dictionary,brackets,Python 3.x,Dictionary,Brackets,我想知道如何管理该键值的括号[]。例如,一本名为“diamond”的词典包含={'a':'Roger','c':'Rafael','b':'Roger'}。我应该重新组织diamond中的数据,使其成为rediamond={'Rafael':['c'],'Roger':'['a','b']} 我的代码 def group_by_owners(files): store = dict() for key,value in files.items(): if value in store:

我想知道如何管理该键值的括号[]。例如,一本名为“diamond”的词典包含={'a':'Roger','c':'Rafael','b':'Roger'}。我应该重新组织diamond中的数据,使其成为rediamond={'Rafael':['c'],'Roger':'['a','b']}

我的代码

def group_by_owners(files):
store = dict()
for key,value in files.items():
    if value in store:
        store[value]=(store[value], [key])
    else:
        store[value]=[key]
return store

files = {
'a': 'Rafael',
'b': 'Roger',
'c': 'Roger'
}   
print(group_by_owners(files))
我的输出

{'Rafael': (['a']), 'Roger': ['b'],['c']}
预期产量

{{'Rafael': (['a']), 'Roger': ['b','c']}

因此,如果罗杰有3个值,它的组织应该像['',''

您应该使用
defaultdict

from collections import defaultdict

diamond = {'a':'Roger', 'c':'Rafael', 'b':'Roger'}

rediamond = defaultdict(list)

for k, v in diamond.items():
    rediamond[v].append(k)

您应该使用
defaultdict

from collections import defaultdict

diamond = {'a':'Roger', 'c':'Rafael', 'b':'Roger'}

rediamond = defaultdict(list)

for k, v in diamond.items():
    rediamond[v].append(k)