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())
精彩评论