我想探索一个包含多个值的字典 例如,function_test(groups)会给我一个结果:
{153: {’sport’, ’travaux manuels’, ’cinephiles’, ’cuisine’},
538: {’sport’}, 147: {’cinephiles’, ’cuisine’},
802: {’sport’, ’travaux manuels’, ’cinephiles’, ’cuisine’},
125: {’travaux manuels’, ’cinephiles’}}
问题是,我不知道如何探索词典的价值以及如何为新词典赋予新的价值。
# membres_hobbies : dict[int:set[str]]
membres_hobbies = {153:{'patisserie','sushi','natation','Almodovar'},
125:{'Harry Potter','dentelle','cinema'},
538:{'boxe','cyclisme','judo'},
147:{'HBO','Harry Potter','sushi'},
802:{'bricolage','cyclisme','Almodovar','sushi'}}
# groupes : dict[str:set[int]]
groupes = {'cinephiles':{802,125,147,153},
'travaux manuels':{125,802,153},
'cuisine':{153,147,802},
'sport':{153,538,802}}
def te(group):
""" Problème Dictionnaire Question 1
dict[str:set[int]] -> dict[int:set[str]]
"""
# D : dict[int:set[str]]
D = {}
# (k, v, w) : tuple[str, int, int]
for k,v in group:
D[v] = D[v] + k
return D
答案 0 :(得分:1)
假设字典保存如下
x = {153: {'sport', 'travaux manuels', 'cinephiles', 'cuisine'},
538: {'sport'}, 147: {'cinephiles', 'cuisine'},
802: {'sport', 'travaux manuels', 'cinephiles', 'cuisine'},
125: {'travaux manuels', 'cinephiles'}}
要访问字典中的元素,您可以使用键。您可以通过
提取您的密钥列表list(x.keys())
[153,538,147,802,125]
现在,如果您想要与键相关联的值,可以通过
访问它们x[153]
{'cinephiles','cuisine','sport','travaux manuels'}
要将带有值的新键添加到您执行的字典
x.update({100: {'wow', 'un', 'autre', 'francais'}})
x[100]
{'autre','francais','un','哇'}
这会在哈希地址100处添加一个新集。
您的词典包含集合。您也可以通过
为这些集添加值x[100].update({'bonjour'})
x[100]
{'autre','bonjour','francais','un','哇'}
这将使用值100对字典元素进行哈希处理。然后使用新值更新设置。
您的具体案例可以通过
解决dic = {}
for key in groupes:
for val in groupes[key]:
if val in dic: dic[val].update({key})
else: dic.update({val: {key}})
使用列表理解
groupes = {'cinephiles':{802,125,147,153},
'travaux manuels':{125,802,153},
'cuisine':{153,147,802},
'sport':{153,538,802}}
dic = {}
[dic[val].update({key}) if val in dic else dic.update({val: {key}})
for key in groupes
for val in groupes[key]]
dic
{p> {125:{'cinephiles','travaux manuels'},
147:{'cinephiles', 'cuisine'},151:{'cinephiles','cuisine','sport','travaux manuels'},
538:{'sport'},802:{'cinephiles','cuisine','sport', 'travaux manuels'}}
答案 1 :(得分:0)
你想要的是一个反向索引,这里是如何在这种特定情况下创建一个。
groupes = {
'cinephiles':{802,125,147,153},
'travaux manuels':{125,802,153},
'cuisine':{153,147,802},
'sport':{153,538,802}
}
def function_test(groups):
result = {}
# You can loop through key-value pairs of a dictionary using dict.items()
for hobby, id_set in groups.items():
# You can loop through a set normally
for id in id_set:
try:
# Add the key, by example 'sports', to the id entry
result[id].add(hobby)
except KeyError:
# If the id is new, the above will fail and we need to create a set
# You can assign to a dictionnary like this
result[id] = {hobby}
return result
function_test(groupes)
事实证明,Python可以通过相当可读的oneliner实现这一目标。
result = {
id: set(k for k in groupes.keys() if id in groupes[k])
for id in set.union(*groupes.values())
}
两种解决方案输出:
{153: {'travaux manuels', 'cinephiles', 'cuisine', 'sport'},
802: {'travaux manuels', 'cinephiles', 'cuisine', 'sport'},
147: {'cinephiles', 'cuisine'},
125: {'travaux manuels', 'cinephiles'},
538: {'sport'}}