Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythonlists-使用一个函数将现有列表列表转换为每个键具有多个值的dict_Python_Loops_Nested_List Comprehension_Dictionary Comprehension - Fatal编程技术网

pythonlists-使用一个函数将现有列表列表转换为每个键具有多个值的dict

pythonlists-使用一个函数将现有列表列表转换为每个键具有多个值的dict,python,loops,nested,list-comprehension,dictionary-comprehension,Python,Loops,Nested,List Comprehension,Dictionary Comprehension,我有一个相对简单的问题,可以通过setdefault轻松解决,但我现在正在自学理解,不知道如何通过理解来做到这一点 假设我有一个嵌套列表,其中一些内部列表具有相同的键。这意味着我应该能够生成一个dict,其中一些键有多个值。我一直试图以某种方式附加这些值,但每次它都只返回最后一个单位数的值,或者给出一个错误 以下是一个例子: >>> strlist ['hello w', 'hello', 'hello c', 'hello c c', 'dog'] >>>

我有一个相对简单的问题,可以通过
setdefault
轻松解决,但我现在正在自学理解,不知道如何通过理解来做到这一点

假设我有一个嵌套列表,其中一些内部列表具有相同的键。这意味着我应该能够生成一个dict,其中一些键有多个值。我一直试图以某种方式附加这些值,但每次它都只返回最后一个单位数的值,或者给出一个错误

以下是一个例子:

>>> strlist
['hello w', 'hello', 'hello c', 'hello c c', 'dog']

>>> [[k,v] for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x]
[['hello', 0], ['hello', 1], ['hello', 2], ['hello', 3], ['w', 0], ['c', 2], ['c', 3], ['dog', 4]]
我还尝试了一个元组列表,一个元组列表,一组列表,一组元组等等,但仍然无法理解

以下是一些失败的尝试:

>>> dict([(k,v) for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x])
{'hello': 3, 'w': 0, 'c': 3, 'dog': 4}

>>> {k:k[v] for k,v in [[k,v] for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x]}
Traceback (most recent call last):
  File "<pyshell#285>", line 1, in <module>
    {k:k[v] for k,v in [[k,v] for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x]}
  File "<pyshell#285>", line 1, in <dictcomp>
    {k:k[v] for k,v in [[k,v] for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x]}
IndexError: string index out of range

>>> {k:{v} for k,v in [[k,v] for k in set(sum([x.split() for x in strlist],[])) for v,x in enumerate(strlist) if k in x]}
{'hello': {3}, 'w': {0}, 'c': {3}, 'dog': {4}}

这在理解中是可能的,还是我必须使用一种更常见的传统循环方法?

使用您的方法将strlist转换为列表列表,然后您可以使用
集合。defaultdict

from collections import defaultdict

lst = [
    ["hello", 0],
    ["hello", 1],
    ["hello", 2],
    ["hello", 3],
    ["w", 0],
    ["c", 2],
    ["c", 3],
    ["dog", 4],
]   

d = defaultdict(set)
for x, y in lst:
    d[x].add(y)
    
print(d)
# defaultdict(<class 'set'>, {'hello': {0, 1, 2, 3}, 'w': {0}, 'c': {2, 3}, 'dog': {4}})

这是一种一行完成的方法,使用听写理解。但是,就效率而言,我不确定这是否是比更传统的for循环更好的选择,使用
setdefault()
和一些临时列表

list_ = ['hello w', 'hello', 'hello c', 'hello c c', 'dog']
result = {
    word: {i for i, phrase in enumerate(list_) if word in phrase}
    for string in list_
    for word in string.split()
}
结果的内容:

{'hello': {0, 1, 2, 3}, 'w': {0}, 'c': {2, 3}, 'dog': {4}}

什么是strlist?
>strlist=['hello w','hello','hello c','hello c','dog']
在一行中做是一种强迫吗?是的,但只是出于好奇,因为我正在学习dict-comprehensions。谢谢你的好提示,但我真的想看看用dict是否可以做到这一点-comprehension@user1026169,请找同样的。编辑。非常感谢您的解释。这是一个完全不切实际的例子,但它帮助我将发生的事情形象化:
{k:set(y代表x,y在[[k,v]代表k在set(sum([x.split()代表x在strlist],])对于v,x在enumerate(strlist)中(strlist)如果k在x中]如果x==k,对于k在set中(sum([x.split()代表x在strlist中),])对于v,x在enumerate(strlist)中(strlist)如果k在x中]}
我只想告诉您,单行代码并不总是适用于所有情况。更重要的是可读性。我建议将其分解为多行,这可能有助于您在稍后阶段进行调试。非常感谢您的澄清。这只是为了满足我对一行理解的好奇心。这些帖子帮助很大。谢谢,这有助于我更好地理解听写理解。非常感谢。
list_ = ['hello w', 'hello', 'hello c', 'hello c c', 'dog']
result = {
    word: {i for i, phrase in enumerate(list_) if word in phrase}
    for string in list_
    for word in string.split()
}
{'hello': {0, 1, 2, 3}, 'w': {0}, 'c': {2, 3}, 'dog': {4}}