【问题标题】:How to join dictionaries created with for loop? [closed]如何加入使用 for 循环创建的字典? [关闭]
【发布时间】:2015-10-04 06:55:36
【问题描述】:

我是 Python 新手。

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

给定一个列表:

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

我想找到,对于每个,比如说,“a”,每个项目都遵循的百分比。代码返回:

[(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 逻辑,所以如果可能的话,我不想使用外部库。

提前谢谢你!

尼科洛

【问题讨论】:

  • 不能max(range(len(list1))) 只是len(list1)
  • 一个 dict 可以使用 update 函数与另一个 dict 组合,即 dict1.update(dict2) 并且这会更新 dict1。如果 dict1 和 dict2 有一个共同的键,则 dict2 中该键的值获胜。
  • @SuperBiasedMan 你是对的,但更准确地说是 len(list1)-1。他想要最后一个元素的索引。
  • 你只是想做this吗?您提出问题的代码太多(meta 上也注意到了这一点)。
  • TMC(代码太多)...

标签: python list loops dictionary iteration


【解决方案1】:

使用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')]

【讨论】:

  • 感谢您的建议,但它给了我:回溯(最近一次调用最后一次):文件“”,第 1 行,在 函数(list1)文件“”,第 31 行,在函数中 d.update(dict([(m, n) for n, m in i])) 文件“”,第 31 行,在 中update(dict([(m, n) for n, m in i])) TypeError: 'int' object is not iterable
  • 谢谢你,现在我开始工作了。
  • 好答案。请添加collections.Counter 的文档链接。
【解决方案2】:

以下内容将汇总您的密钥并计算百分比:

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)]

【讨论】:

    【解决方案3】:

    如果您需要列表中每个项目的唯一追随者,您可以考虑仅获取每个项目的第一次出现,然后计算其后的项目,在这种情况下,项目“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'
    

    【讨论】:

    • 我听不懂你的回答。使用您的代码,我无法选择目标项目,在我看来,它就像一个频率计数器,因为我得到:a 有 100.0 % 的关注者 b 有 75.0 % 的关注者 c 有 50.0 % 的关注者 d 有 25.0 % 的关注者 e 有 0.0 % 的关注者和“e 有 0.0% 的追随者”对我来说似乎也不正确。
    • e 有 0.0% 的关注者意味着 e 之后没有新项目出现。我已经修改了我的答案,以包括出现在给定元素之后的每个项目的百分比。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多