0

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

4

6 回答 6

2

使用默认字典。

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模块来解析文件中的列,但在这种情况下,这对我来说似乎有点过分了。

于 2012-08-14T20:44:52.900 回答
2

计数器是专门为此设计的。

# 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]))
于 2012-08-14T20:46:36.060 回答
0
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}
于 2012-08-14T20:55:05.960 回答
0
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
于 2012-08-14T20:47:21.940 回答
0
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,但上面应该可以工作,可能需要稍作调整)

于 2012-08-14T20:47:51.907 回答
0

不喜欢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
于 2012-08-15T13:31:34.583 回答