作者ypcat ((ノ‵□′)ノ┴┴)
看板Python
标题Re: [问题] txt内容切割加总
时间Tue Oct 28 22:04:32 2014
※ 引述《PTT007 (优质单身好男人)》之铭言:
: 如果我有一个txt内容如下:
: 0.01
: 0.02
: 0.03
: 0.09
: 1.03
: 1.02
: 1.01
: 我想将 0.X 和 1.X 各自累加起来
: 输出结果为 0.15 和 3.06
# a functional solution
import itertools
for key, group in itertools.groupby(map(float, open('in.txt')), int):
print key, sum(group)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.135.71.186
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1414505076.A.2BC.html
1F:推 alibuda174: great 10/29 01:00
2F:推 apua: 正解!! 11/06 10:20
3F:→ apua: 不过资料必须是事先按整数排序过~ 11/06 11:03