Python:合并统计数据 [英] Python: merging tally data
问题描述
好吧,我确信这已经在这里回答过了,但我找不到它....
Okay - I'm sure this has been answered here before but I can't find it....
我的问题:我有一个列表与此组合
My problem: I have a list of lists with this composition
0.2 A
0.1 A
0.3 A
0.3 B
0.2 C
0.5 C
我的目标是输出以下内容:
My goal is to output the following:
0.6 A
0.3 B
0.7 C
换句话说,我需要将数据合并在一起。
In other words, I need to merge the data from multiple lines together.
这是我使用的代码:
unique_percents = []
for line in percents:
new_percent = float(line[0])
for inner_line in percents:
if line[1] == inner_line[1]:
new_percent += float(inner_line[0])
else:
temp = []
temp.append(new_percent)
temp.append(line[1])
unique_percents.append(temp)
break
我认为它应该可以工作,但它不是添加百分比,仍然有重复。也许我不明白休息如何运作?
I think it should work, but it's not adding the percents up and still has the duplicates. Perhaps I'm not understanding how "break" works?
我还将提出一个更好的循环结构或算法来使用。谢谢大卫。
I'll also take suggestions of a better loop structure or algorithm to use. Thanks, David.
推荐答案
你想使用一个dict,但是 collections.defaultdict
可以非常方便地在这里,所以你不必担心该键是否存在于dict中 - 它默认为0.0:
You want to use a dict, but collections.defaultdict
can come in really handy here so that you don't have to worry about whether the key exists in the dict or not -- it just defaults to 0.0:
import collections
lines = [[0.2, 'A'], [0.1, 'A'], [0.3, 'A'], [0.3, 'B'], [0.2, 'C'], [0.5, 'C']]
amounts = collections.defaultdict(float)
for amount, letter in lines:
amounts[letter] += amount
for letter, amount in sorted(amounts.iteritems()):
print amount, letter
这篇关于Python:合并统计数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!