python获取每个键的唯一值计数和键中唯一的值计数

时间:2017-05-15 13:37:49

标签: python dictionary

我有以下字典:

dictA = { 
    'a' : ['duck','duck','goose'], 
    'b' : ['goose','goose'], 
    'c' : ['duck','duck','duck'], 
    'd' : ['goose'], 
    'e' : ['duck','goose'] 
    }

我想得到以下结果:

{
    'duck': {'countALL':3, 'countDoc': {'a': 2, 'b': 0, 'c': 3, 'd': 0, 'e':1}},
    'goose': {'countALL':4, 'countDoc': {'a': 1, 'b': 2, 'c': 0, 'd': 1, 'e':1}},
}   

3 个答案:

答案 0 :(得分:6)

您可以执行以下操作:

unique_items = set(x for y in dictA.values() for x in y)

new_dict = {}
for item in unique_items:
    new_dict[item] = {'countALL': sum(1 for x in dictA if item in dictA[x]), 'countDoc': {k: v.count(item) for k, v in dictA.items()}}
print(new_dict)
# {'goose': {'countALL': 4, 'countDoc': {'e': 1, 'a': 1, 'c': 0, 'b': 2, 'd': 1}}, 'duck': {'countALL': 3, 'countDoc': {'e': 1, 'a': 2, 'c': 3, 'b': 0, 'd': 0}}}

请注意,内部字典上的顺序是 random

这有两点有趣:

  • 生成器表达式,用于计算包含item的列表:sum(1 for x in dictA if item in dictA[x])

  • 以及在每个列表中得到计数的字典理解:{k: v.count(item) for k, v in dictA.items()}

但它们都比较容易阅读,所以我现在就把它留给你。如果您有任何问题,请随时询问。

答案 1 :(得分:2)

如果列表非常大,首先创建collection.Counter个实例可能会有所回报,而不是重复调用>>> words = set(word for lst in dictA.values() for word in lst) >>> counts = {k: Counter(v) for k, v in dictA.items()} >>> counts {'a': Counter({'duck': 2, 'goose': 1}), 'd': Counter({'goose': 1}), 'b': Counter({'goose': 2}), 'c': Counter({'duck': 3}), 'e': Counter({'goose': 1, 'duck': 1})}

>>> {word: {"countALL": sum(word in c for c in counts.values()), 
...         "countDoc": {k: v.get(word, 0) for k, v in counts.items()}}
...  for word in words}
{'duck':  {'countALL': 3, 'countDoc': {'a': 2, 'd': 0, 'b': 0, 'c': 3, 'e': 1}},
 'goose': {'countALL': 4, 'countDoc': {'a': 1, 'd': 1, 'b': 2, 'c': 0, 'e': 1}}}

然后可以使用“简单的”嵌套字典理解来组装结果:

var t = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));

答案 2 :(得分:1)

dictA = { 
    'a' : ['duck','duck','goose'], 
    'b' : ['goose','goose'], 
    'c' : ['duck','duck','duck'], 
    'd' : ['goose'], 
    'e' : ['duck','goose'] 
    }

dictB = {}
mySetOfKeys=set()

for myKey in dictA:
    mySetOfKeys.add(myKey)
    for myBird in dictA[myKey]:
        if dictB.has_key(myBird):
            if dictB[myBird]['countDoc'].has_key(myKey):
                dictB[myBird]['countDoc'][myKey] += 1
            else:
                dictB[myBird]['countALL'] += 1
                dictB[myBird]['countDoc'][myKey] = 1
        else:
            dictB[myBird] = {'countALL':1,'countDoc':{myKey:1}}

for myBird in dictB:
    for myKey in mySetOfKeys:
        if dictB[myBird]['countDoc'].has_key(myKey):
            pass
        else:
            dictB[myBird]['countDoc'][myKey] = 0
print dictB
print mySetOfKeys

    enter code here