Python:合并理货数据
好的-我肯定这个问题以前在这里已经得到了回答,但我找不到它 我的问题是:我有一个这样的列表 0.2 A 0.1 A 0.3 A 0.3 B 0.2摄氏度 0.5摄氏度 我的目标是输出以下内容: 0.6 A 0.3 B 0.7摄氏度 换句话说,我需要将多行数据合并在一起 以下是我使用的代码:Python:合并理货数据,python,loops,duplicates,break,nested-loops,Python,Loops,Duplicates,Break,Nested Loops,好的-我肯定这个问题以前在这里已经得到了回答,但我找不到它 我的问题是:我有一个这样的列表 0.2 A 0.1 A 0.3 A 0.3 B 0.2摄氏度 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 perce
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
我认为它应该是有效的,但它没有把百分比加起来,仍然有重复项。也许我不明白“休息”是怎么工作的
我还将考虑使用更好的循环结构或算法的建议。谢谢你,大卫。如果你有这样的列表:
[[0.2,A],[0.1,A],…]
(实际上它看起来像一个元组列表:)
您想要使用dict,但是集合。defaultdict在这里非常方便,这样您就不必担心dict中是否存在密钥,它只是默认为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
用于计算值
(假设文本数据位于d
):
>s=collections.defaultdict(float)
>>>对于d中的ln:
... v、 k=ln.split()
... s[k]+=浮点数(v)
>>>
defaultdict(,{'A':0.6000000000000009,'C':0.69999999996,'B':0.2999999999999})
>>>[%s%s”%(v,k)表示s.iteritems()中的k,v]
['0.6 A'、'0.7 C'、'0.3 B']
>>>
这是冗长的,但有效:
# Python 2.7
lines = """0.2 A
0.1 A
0.3 A
0.3 B
0.2 C
0.5 C"""
lines = lines.split('\n')
#print(lines)
pctg2total = {}
thing2index = {}
index = 0
for line in lines:
pctg, thing = line.split()
pctg = float(pctg)
if thing not in thing2index:
thing2index[thing] = index
index = index + 1
pctg2total[thing] = pctg
else:
pctg2total[thing] = pctg2total[thing] + pctg
output = ((pctg2total[thing], thing) for thing in pctg2total)
# Let's sort by the first occurrence.
output = list(sorted(output, key = lambda thing: thing2index[thing[1]]))
print(output)
>>>
[(0.60000000000000009, 'A'), (0.29999999999999999, 'B'), (0.69999999999999996, 'C')]
由于所有字母等级都分组在一起,因此可以使用itertools.groupby(如果没有,只需提前对列表进行排序即可): 给出:
{'A': 0.60000000000000009, 'C': 0.69999999999999996, 'B': 0.29999999999999999}
如果您使用的是Python3.1或更新版本,则可以使用。此外,我建议使用以下替代浮点数:
# Counter requires python 3.1 and newer
from collections import Counter
from decimal import Decimal
lines = ["0.2 A", "0.1 A", "0.3 A", "0.3 B", "0.2 C", "0.5 C"]
results = Counter()
for line in lines:
percent, label = line.split()
results[label] += Decimal(percent)
print(results)
结果是:
计数器({'C':十进制('0.7'),'A':十进制('0.6'),'B':十进制('0.3'))
假设我们有这个
data =[(b, float(a)) for a,b in
(line.split() for line in
"""
0.2 A
0.1 A
0.3 A
0.3 B
0.2 C
0.5 C""".splitlines()
if line)]
print data
# [('A', 0.2), ('A', 0.1), ('A', 0.3), ('B', 0.3), ('C', 0.2), ('C', 0.5)]
你现在可以通过这个来求和
counter = {}
for letter, val in data:
if letter in counter:
counter[letter]+=val
else:
counter[letter]=val
print counter.items()
或将值组合在一起并使用总和:
from itertools import groupby
# you want the name and the sum of the values
print [(name, sum(value for k,value in grp))
# from each group
for name, grp in
# where the group name of a item `p` is given by `p[0]`
groupby(sorted(data), key=lambda p:p[0])]
试试这个:
result = {}
for line in percents:
value, key = line
result[key] = result.get(key, 0) + float(value)
有Python字典吗?如果是C#,我会用这个。是决赛季还是什么?我会在这里用字典。查Python字典。它们正是你们需要的。谢谢大家。我确实“知道”字典,但我只是不知道在这种情况下如何使用它们。许多好的答案都很有帮助。哦,而且是盲目的,我在自学。这就是非常愚蠢的问题。+1我喜欢带有默认值的get()调用。很高兴能够将它保持在一行。按b c排序如何?使用OrderedDict-这很整洁,排序如何?使用运算符:key=operator.itemgetter(1)很酷!这就是我喜欢Python的原因,它从未停止让你惊讶:)(P.s.不知道collections.defaultdict)
>>> from itertools import groupby, imap
>>> from operator import itemgetter
>>> data = [['0.2', 'A'], ['0.1', 'A'], ['0.3', 'A'], ['0.3', 'B'], ['0.2', 'C'], ['0.5', 'C']]
>>> # data = sorted(data, key=itemgetter(1))
...
>>> for k, g in groupby(data, key=itemgetter(1)):
... print sum(imap(float, imap(itemgetter(0), g))), k
...
0.6 A
0.3 B
0.7 C
>>>
# Counter requires python 3.1 and newer
from collections import Counter
from decimal import Decimal
lines = ["0.2 A", "0.1 A", "0.3 A", "0.3 B", "0.2 C", "0.5 C"]
results = Counter()
for line in lines:
percent, label = line.split()
results[label] += Decimal(percent)
print(results)
data =[(b, float(a)) for a,b in
(line.split() for line in
"""
0.2 A
0.1 A
0.3 A
0.3 B
0.2 C
0.5 C""".splitlines()
if line)]
print data
# [('A', 0.2), ('A', 0.1), ('A', 0.3), ('B', 0.3), ('C', 0.2), ('C', 0.5)]
counter = {}
for letter, val in data:
if letter in counter:
counter[letter]+=val
else:
counter[letter]=val
print counter.items()
from itertools import groupby
# you want the name and the sum of the values
print [(name, sum(value for k,value in grp))
# from each group
for name, grp in
# where the group name of a item `p` is given by `p[0]`
groupby(sorted(data), key=lambda p:p[0])]
result = {}
for line in percents:
value, key = line
result[key] = result.get(key, 0) + float(value)