If I have sample input:
Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,2
How can I get output like following:
Apples,7
Oranges,12
Thanks.
Edit: I'm using Python 3
使用默认字典。
from collections import defaultdict
d=defaultdict(int)
with open('inputfile') as f:
for line in f:
fruit,num = line.split(',')
#could also use `fruit,num,*rest = line.split(',')`
#for a little more robustness at the expense of
#backward compatability
d[fruit]+=int(num)
您也可以使用该csv
模块来解析文件中的列,但在这种情况下,这对我来说似乎有点过分了。
计数器是专门为此设计的。
# I'm assuming you have a list or iterable of lines like this
file="""Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,2""".split('\n')
# Here's the code under that assumtion
from collections import Counter
from functools import reduce
from operator import add
tally = reduce(add, (Counter({x[0]: int(x[1])}) for x in (x.split(',') for x in file)))
# To demonstrate
for name in tallies:
print('%s,%s' % (name, tallies[name]))
strs="""Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,2"""
dic={}
for x in strs.split():
key=x.split(',')[0]
val=int(x.split(',')[1])
dic[key]=dic.get(key,0)+val
print(dic) #prints {'Apples': 7, 'Oranges': 12}
from collections import defaultdict
result = defaultdict(int)
lines = """
Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,12
"""
for k in lines.splitlines():
result[k.split(",")[0]]+=int(k.split(",")[-1] or 0)#account for '' cases...
print result
stuff = """
Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,2
"""
# Holds mapping from fruit->count
total = {}
# Process input
lines = stuff.strip().splitlines()
for x in lines:
name, _, count = x.partition(",")
total.setdefault(name, 0)
total[name] += int(count)
# Output
for k, v in sorted(total.items()):
print("{},{}".format(k, v))
(这台机器上没有安装 Python 3,但上面应该可以工作,可能需要稍作调整)
不喜欢groupby?
data = """\
Apples,2
Apples,4
Apples,1
Oranges,10
Oranges,2""".splitlines()
from itertools import groupby
groups = groupby((line.split(',') for line in data), key=lambda x:x[0])
table = dict((k,sum(map(int,(x[1] for x in vals)))) for k,vals in groups)
for item in table.items():
print '%s,%d' % item
印刷:
Apples,7
Oranges,12