[英]How do I create dictionary keys from list of lists?
List of strings:字符串列表:
['Georgie Porgie', 87, '$$$', ['Canadian', 'Pub Food'], 'Queen St. Cafe', 82, '$', ['Malaysian', 'Thai'], 'Dumplings R Us', 71, '$', 'Chinese', 'Mexican Grill', 85, '$$', 'Mexican', 'Deep Fried Everything', 52, '$', 'Pub Food']
I am trying to create a cuisine_to_names dictionary out of the list above.我正在尝试从上面的列表中创建一个 food_to_names 字典。 The cuisines are at index 3 and are sometimes a mini-list of their own.这些美食在索引 3 中,有时是它们自己的迷你列表。 The restaurant names are at index 0. They repeat every fourth index.餐厅名称位于索引 0。它们每四个索引重复一次。 cuisines - 3::4, names - 0::4.美食 - 3::4,名称 - 0::4。
The issue that I have is extracting elements out of index 3::4 and making them keys.我遇到的问题是从索引 3::4 中提取元素并将它们设为键。 I think the trouble comes because sometimes they are a mini list of strings and sometimes they are just a string.我认为问题来了,因为有时它们是字符串的迷你列表,有时它们只是一个字符串。 This made using defaultdict difficult for me, but I'm new to learning that function.这让我很难使用 defaultdict,但我不熟悉 function。 I saw some other answers include things like setdefault, but I have no idea how to use that for this specific case.我看到其他一些答案包括 setdefault 之类的东西,但我不知道如何在这种特定情况下使用它。 Any guidance would be appreciated!任何指导将不胜感激!
I would like this output:我想要这个 output:
Cuisine to list of restaurant names:
# dict of {str, list of str}
{'Canadian': ['Georgie Porgie'],
'Pub Food': ['Georgie Porgie', 'Deep Fried Everything'],
'Malaysian': ['Queen St. Cafe'],
'Thai': ['Queen St. Cafe'],
'Chinese': ['Dumplings R Us'],
'Mexican': ['Mexican Grill']}
I tried this and got TypeError: unhashable type: 'list':我试过这个并得到 TypeError: unhashable type: 'list':
from collections import defaultdict
cuisine_to_name = defaultdict(list)
for cuisine, name in zip(contents_list_2[3::4], contents_list_2[::4]):
cuisine_to_name[cuisine].append(name)
print(cuisine_to_name)
The problem is that the variable cuisine is a list and you are trying to use it as key of the dictionary, do this instead:问题是变量美食是一个列表,并且您试图将其用作字典的键,请改为执行以下操作:
import pprint
from collections import defaultdict
data = ['Georgie Porgie', 87, '$$$', ['Canadian', 'Pub Food'],
'Queen St. Cafe', 82, '$', ['Malaysian', 'Thai'],
'Dumplings R Us', 71, '$', 'Chinese',
'Mexican Grill', 85, '$$', 'Mexican',
'Deep Fried Everything', 52, '$', 'Pub Food']
res = defaultdict(list)
for v, k in zip(data[::4], data[3::4]):
ks = k if isinstance(k, list) else [k]
for ki in ks:
res[ki].append(v)
pprint.pprint(dict(res))
Output Output
{'Canadian': ['Georgie Porgie'],
'Chinese': ['Dumplings R Us'],
'Malaysian': ['Queen St. Cafe'],
'Mexican': ['Mexican Grill'],
'Pub Food': ['Georgie Porgie', 'Deep Fried Everything'],
'Thai': ['Queen St. Cafe']}
If l is your list, you can do the following:如果 l 是您的列表,您可以执行以下操作:
l=['Georgie Porgie', 87, '$$$', ['Canadian', 'Pub Food'],
'Queen St. Cafe', 82, '$', ['Malaysian', 'Thai'],
'Dumplings R Us', 71, '$', 'Chinese',
'Mexican Grill', 85, '$$', 'Mexican',
'Deep Fried Everything', 52, '$', 'Pub Food']
res={l[i]:l[i+3] if type(l[i+3])==list else [l[i+3]] for i in range(0,len(l),4)}
>>>print(res)
{'Georgie Porgie': ['Canadian', 'Pub Food'], 'Queen St. Cafe': ['Malaysian', 'Thai'], 'Dumplings R Us': ['Chinese'], 'Mexican Grill': ['Mexican'], 'Deep Fried Everything': ['Pub Food']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.