简体   繁体   English

如何从列表列表中创建字典键?

[英]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.

相关问题 如何用列表中的键和值分隔空列表创建字典? - How do I create a dictionary with keys from a list and values separate empty lists? 如何从两个列表创建字典,一个列表是键,另一个包含嵌套值? - How can I create a dictionary from two lists, one list is the keys, the other contains nested values? 如何创建字典外的列表,其名称与字典键相同,并且大小与该键的值相同? - How do I create lists out of dictionary, having same name as dictionary keys and same size as value of that key? 如何从列表中创建一个字典,键是一个迭代数? - How do I create a dictionary from a list with the keys being an iterated number? 我如何创建从不同列表到不同列表的列表字典 - how can i create dictionary of list from to different lists 如何从字典中创建每个键的排列? - How do I create permutations of of each keys from dictionary? 如何从字典列表中创建三个单独的值列表,其中每个字典都有三个键 - How to create three separate lists of values from a list of dictionaries where each dictionary has three keys 如何从 python 中的列表列表创建字典? - How to create a dictionary from a list of lists in python? 如何从列表列表中创建字典并对其进行排序? - How to create a dictionary from a list of lists and sort it? 如何创建以列表为值的字典? - How do I create a dictionary with lists as values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM