开发者

Getting a set of values from a dictionary of sets

What is the preferred method to get the set of values from a dictionary of sets?

I came up with using reduce and itervalues() was wondering if there is a better method.

>>> 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.un开发者_开发知识库ion(y), m_dict.itervalues())
set([1, 2, 4, 5, 8, 9])
>>>

Thanks


set.union can accept multiple sets so, set.union(*m_dict.values())

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜