好的 - 我确定之前已经回答了这个问题,但我找不到它......
我的问题:我有一个列表与这个组成
0.2 A
0.1 A
0.3 A
0.3 B
0.2 C
0.5℃
我的目标是输出以下内容:
0.6 A
0.3 B
0.7℃
换句话说,我需要将来自多行的数据合并在一起.
这是我正在使用的代码:
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
Run Code Online (Sandbox Code Playgroud)
我认为它应该可以工作,但它并没有增加百分比,仍然有重复.也许我不明白"休息"是如何运作的?
我还会提出更好的循环结构或算法的建议.谢谢,大卫.