如何加入用 for 循环创建的字典?

How to join dictionaries created with for loop?

我是 Python 的新人。

我创建了一个代码,可以让我找到列表中给定项目之后的项目百分比。

给定一个列表:

list1=["a", "b", "a", "c", "a", "b", "c", "d", "e", "a", "b", "d", "e", "a", "c"]

我想为每个 "a" 找到每个项目所遵循的百分比。代码returns:

[(33, 'a'), (25, 'b'), (16, 'e'), (16, 'd'), (16, 'c')]
[(30, 'a'), (20, 'e'), (20, 'd'), (20, 'c'), (20, 'b')]
[(25, 'e'), (25, 'd'), (25, 'b'), (25, 'a'), (12, 'c')]
[(33, 'e'), (33, 'd'), (33, 'b'), (33, 'a')]
[]  

输出正确,正是我想要的。
但我也想总结不同词典的每个键,所以我可以有这样的东西:

[(121, 'a'), (103, 'b'), (94, 'e'), (94, 'd'), (48, 'c')]

我没有找到这样做的方法。我知道有一些方法可以对不同词典中每个键的值求和,但这里的问题是词典是在 for 循环内创建的,因为我需要尽可能多的目标项作为词典(在这种情况下,"a").

我试着用

遍历每个字典
   for key, value in dictio.items():
        dictio[key]=value + dictio.get(key, 0)
        print (dictio)

但是结果一塌糊涂,离我想要的也不远了

我想问你是否可以在不知道编号的情况下加入多个词典(因为它们是在 for 循环中创建的)。

而且,因为我想更好地理解 Python 逻辑,所以我不想使用外部库,如果可能的话。

提前致谢!

尼科洛

只是一种使用 Counter

的懒惰方式
from collections import Counter
d = Counter()


mylist = [[(33, 'a'), (25, 'b'), (16, 'e'), (16, 'd'), (16, 'c')],
            [(30, 'a'), (20, 'e'), (20, 'd'), (20, 'c'), (20, 'b')],
            [(25, 'e'), (25, 'd'), (25, 'b'), (25, 'a'), (12, 'c')],
            [(33, 'e'), (33, 'd'), (33, 'b'), (33, 'a')],
            []]

for i in mylist:
    d.update(dict([(m,n) for n,m in i]))
>>>[(j,i) for i,j in d.items()]
[(121, 'a'), (48, 'c'), (103, 'b'), (94, 'e'), (94, 'd')]

排序

>>>sorted([(j,i) for i,j in d.items()], key=lambda x:x[1])
[(121, 'a'), (103, 'b'), (48, 'c'), (94, 'd'), (94, 'e')]

获取百分比(假设)

>>>[(j*100/sum(d.values()),i) for i,j in d.items()] # caution==> sum(d.values()) save in a variable, otherwise it will execute in every iteration
[(26, 'a'), (10, 'c'), (22, 'b'), (20, 'e'), (20, 'd')]

以下将对您的密钥求和并计算百分比:

import collections, itertools

d = collections.Counter()

mylist = [[(33, 'a'), (25, 'b'), (16, 'e'), (16, 'd'), (16, 'c')],
            [(30, 'a'), (20, 'e'), (20, 'd'), (20, 'c'), (20, 'b')],
            [(25, 'e'), (25, 'd'), (25, 'b'), (25, 'a'), (12, 'c')],
            [(33, 'e'), (33, 'd'), (33, 'b'), (33, 'a')],
            []]

for count, item in itertools.chain.from_iterable(mylist):
    d.update(itertools.repeat(item, count))

print "Usage order:", d.most_common()
lsorted = sorted(d.items())
print "Key order:", lsorted

total = sum(d.values())
print "Percentages:", [(key, (value * 100.0)/total) for key,value in lsorted]

给予:

Usage order: [('a', 121), ('b', 103), ('e', 94), ('d', 94), ('c', 48)]
Key order: [('a', 121), ('b', 103), ('c', 48), ('d', 94), ('e', 94)]
Percentages: [('a', 26.304347826086957), ('b', 22.391304347826086), ('c', 10.434782608695652), ('d', 20.434782608695652), ('e', 20.434782608695652)]

如果列表中的每个项目都需要唯一的关注者,您可以考虑只取每个项目的第一次出现,然后计算它之后的项目,在这种情况下,项目 "e" 之后不会有新项目 (0 %)。但是,如果问题是给定元素之后某个项目的出现次数,我将按如下方式进行:

list1=["a", "b", "a", "c", "a", "b", "c", "d", "e", "a", "b", "d", "e", "a", "c"]
indexlist=[list1.index(item) for item in list(set(list1))]
newlist=[list1[j] for j in sorted(indexlist)]

for item in newlist:
    print '\n',item,'Followers:'
    a=list1[list1.index(item)+1:]
    for follower in a:
        if item!=follower:
            fol=(follower,Counter.get(Counter(a),follower)*100.0/Counter.get(Counter(list1),follower))
    print fol,'round'