简体   繁体   English

遍历列表元素的字典

[英]Iterating over a dictionary of list elements

I'm very new to Python (2.x) and trying to understand how to iterate over a dictionary containing multiple lists: 我是Python(2.x)的新手,它试图了解如何遍历包含多个列表的字典:

dict = {'list_1':[3, 'green', 'yellow', 'black'], 'list_2':[2, 'green', 'blue']}

I am trying to create a new list containing all of the unique values of these lists, but ignoring the first item (the integer). 我正在尝试创建一个包含这些列表的所有唯一值的新列表,但忽略了第一项(整数)。 The result I'm looking for would be: 我正在寻找的结果是:

['green', 'yellow', 'black', 'blue']

Here is one of my many attempts. 这是我的许多尝试之一。 I'm quite lost, so if somebody could explain I would be most grateful. 我很迷茫,所以如果有人可以解释我将不胜感激。

newlist = []
for colors in dict.values() [1:]:
    if not colors in newlist:
        newlist.append(colors)

Use set.union : 使用set.union

>>> dic = {'list_1':[3, 'green', 'yellow', 'black'], 'list_2':[2, 'green', 'blue']}
>>> set().union(*(x[1:] for x in dic.itervalues()))
set(['blue', 'black', 'green', 'yellow'])

If a list is required simply pass this set to list() . 如果需要列表,只需将此设置传递给list()

The working version of your attempt, though it is not efficient; 您尝试的有效版本,尽管效率不高;

newlist = []
for colors in dic.values():
    lis = colors[1:]        #do slicing here
    for item in lis:
        if item not in newlist:
            newlist.append(item)
print newlist #['green', 'blue', 'yellow', 'black']

One way using itertools.chain to flatten the dict values into a single list then list comprehension to filter out the non-string values and finally set for the unique values: 一种使用itertools.chain将dict值展平为单个列表,然后使用list comprehension来过滤掉非字符串值并最终set唯一值的一种方法:

In [1]: from itertools import chain
In [2]: dict={'list_1':[3,'green','yellow','black'],'list_2':[2,'green','blue']}
In [3]: set([x for x in chain(*dict.values()) if isinstance(x, str)])
Out[3]: set(['blue', 'black', 'green', 'yellow'])

If really want to remove the first item in the list only and not all ints then similarly you could do: 如果确实只想删除列表中的第一项而不是所有int,则可以类似地执行以下操作:

In [4]: set(chain(*[x[1:] for x in dict.itervalues()]))
Out[4]: set(['blue', 'black', 'green', 'yellow'])

The first answers fails if you want all ints not in the first position in the final set and second fails for none ints found at the first position so you should state what should happen for these cases. 如果您希望所有int不在最终集中的第一个位置,则第一个答案将失败,而如果您在第一个位置没有找到任何int,则第二个答案将失败,因此您应说明在这些情况下会发生什么。

This would work, even if you have integer at some other index: 即使您在其他某个索引处使用了整数,这也将起作用:

>>> di = {'list_1':[3, 'green', 'yellow', 'black'], 'list_2':[2, 'green', 'blue']}
>>> set(x for value in di.values() for x in value if not isinstance(x, int))
set(['blue', 'black', 'green', 'yellow'])

If you print dict.values() , you get: 如果打印dict.values() ,则会得到:

[[2, 'green', 'blue'], [3, 'green', 'yellow', 'black']]

Hence, when you're trying to slice it, you get: 因此,当您尝试对其进行切片时,您将获得:

[[3, 'green', 'yellow', 'black']]

Thus, you want to slice colors instead of the dict.values() : 因此,您要分割colors而不是dict.values()

for colors in t.values():
    colors = colors[1:]

Now there's another problem. 现在还有另一个问题。 You're checking if the list has been seen in the list, instead of each item. 您正在检查列表中是否已看到该列表,而不是每个项目。 So you have to loop again . 因此,您必须再次循环。

for colors in t.values():
    colors = colors[1:]
    for color in colors:

Note that this can lead your script to not have such great performance, so you could probably do something like: 请注意,这可能会导致脚本性能不佳,因此您可以执行以下操作:

>>> from itertools import chain
>>> [i[1:] for i in t.values()]
[['green', 'blue'], ['green', 'yellow', 'black']]
>>> list(chain.from_iterable(i[1:] for i in t.values()))
['green', 'blue', 'green', 'yellow', 'black']

Therefore, integrated into your code: 因此,集成到您的代码中:

new = set() # Better for performance to use sets
for color in chain.from_iterable(i[1:] for i in t.values()):
    if color not in new:
        new.add(color)

print new

Prints: 打印:

set(['blue', 'black', 'green', 'yellow']) # If you need order, use a list instead.

By the way, don't name a dictionary dict . 顺便说一句,不要给字典dict命名。 It overrides the built-in type. 它会覆盖内置类型。

theColours = set()
for colours in d.values():
    [theColours.add(colour) for colour in colours[1:]]
list(theColours)

here's one-liner for you 这是给你的一线

>>> d = {'list_1':[3, 'green', 'yellow', 'black'], 'list_2':[2, 'green', 'blue']}
>>> list(set(i for x in d.itervalues() for i in x[1:]))
['blue', 'black', 'green', 'yellow']

As Haidro says, don't name a dictionary dict 如Haidro所说,不要命名字典dict

创建一个删除重复项的set ,然后通过内置的sorted()运行它以返回列表。

newlist = sorted(set(dict.values()[1:]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM