从集合字典中获取一组值

时间:2011-05-08 06:39:15

标签: python dictionary set

从集合字典中获取值集的首选方法是什么?

我想出了reduce并且itervalues()想知道是否有更好的方法。

>>> m_dict = { 'a': set([1,2]),
... 'b': set([1,4,5]),
... 'c': set([2,8,9]) }
>>> print m_dict
{'a': set([1, 2]), 'c': set([8, 9, 2]), 'b': set([1, 4, 5])}
>>> reduce(lambda x,y:x.union(y), m_dict.itervalues())
set([1, 2, 4, 5, 8, 9])
>>>

由于

1 个答案:

答案 0 :(得分:4)

set.union可以接受多个集合,set.union(*m_dict.values())