作者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://www.ptt.cc/bbs/Python/M.1414505076.A.2BC.html
推 alibuda174: great 10/29 01:00
推 apua: 正解!! 11/06 10:20
→ apua: 不過資料必須是事先按整數排序過~ 11/06 11:03