反对独特的价值观Python

时间:2013-09-26 14:39:26

标签: python

你好,我有一个包含这样的数据的反制词:

{1301: Counter({'total': 18, 
               'inDevelopment': 13, 
                'isDuplicate': 2, 
                'inAnalysis': 2,
                'inQuest': 1}),
 1302: Counter({'total': 15, 
                'inDevelopment': 9, 
                 'inQuest': 1, 
                 'inValidation': 1, 
                  'inAnalysis': 1,
                  'ongoing' : 3})}

如何在列表中检索其值而不重复。 我的意思是我想要提取所有现有的值,但是我没有将它们全部提取出去,我想让它们 NOT 重复,所以不要这样:

 [' inDevelopment','isDuplicate','inAnalysis', 'inQuest','total', 'inDevelopment','inQuest', 'inValidation','inAnalysis', 'ongoing']

输出如下:

['total','inDevelopment','isDuplicate','inAnalysis','inQuest','inValidation','ongoing']

任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用Counter运算符联合|个对象:

>>> from collections import Counter
>>> a = Counter('123')
>>> b = Counter('44144')
>>> a
Counter({'2': 1, '3': 1, '1': 1})
>>> b
Counter({'4': 4, '1': 1})
>>> a | b
Counter({'4': 4, '2': 1, '3': 1, '1': 1})
>>> list(a | b)
['2', '3', '1', '4']

在Python 2.x

>>> from collections import Counter
>>> d = {1301: Counter({'total': 18,
    ...
...                   "ongoing" : 3})}
>>> list(reduce(lambda a,b:a|b, d.values()))
['inAnalysis', 'inQuest', 'inDevelopment', ' inDevelopment', 'inValidation', 'ongoing', 'isDuplicate', 'total']

在Python 3.x

>>> from collections import Counter
>>> from functools import reduce
>>> d = ...
>>> list(reduce(lambda a,b:a|b, d.values()))
['inValidation', 'total', ' inDevelopment', 'inDevelopment', 'isDuplicate', 'ongoing', 'inQuest', 'inAnalysis']

更新

您还可以使用set.union

>>> list(set().union(*d.values()))
['inValidation', 'inDevelopment', 'isDuplicate', 'total', 'ongoing', 'inAnalysis', 'inQuest', ' inDevelopment']

这在一个代码中的Python 2.x / 3.x中都有效。

答案 1 :(得分:0)

您可以将np.uniqueimport numpy as np

一起使用
>>> d = {1301: Counter({'total': 18,
                'inDevelopment': 13,
                 'isDuplicate': 2,                 'inAnalysis': 2,
                'inQuest': 1}),
  1302: Counter({'total': 15,                 'inDevelopment': 9,
                  'inQuest': 1,
                  'inValidation': 1,
                   'inAnalysis': 1,
                   "ongoing" : 3})}

给出

>>> np.unique(list(d[1301]|d[1302]))

array(['inAnalysis', 'inDevelopment', 'inQuest', 'inValidation',
       'isDuplicate', 'ongoing', 'total'], 
      dtype='|S13')