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. The cuisines are at index 3 and are sometimes a mini-list of their own. The restaurant names are at index 0. They repeat every fourth index. cuisines - 3::4, names - 0::4.
The issue that I have is extracting elements out of index 3::4 and making them keys. 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. I saw some other answers include things like setdefault, but I have no idea how to use that for this specific case. Any guidance would be appreciated!
I would like this 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':
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
{'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=['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']}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.