开发者

Getting a set of values from a dictionary of sets

开发者 https://www.devze.com 2023-03-03 13:03 出处:网络
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.

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

精彩评论

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