我的文本文件包含3列,第一列包含字符串,第二列包含整数,第3列包含浮点,如下所示:
steve 134 0.000010
Harry 133 0.000012
joanne 134 0.000022
steve 135 0.000024
joanne 133 0.000045
steve 133 0.000078
joanne 133 0.000079
joanne 133 0.000081
joanne 136 0.000083
steve 134 0.000085
steve 135 0.000087
我希望输出看起来像这样:
steve 134(count=2,Time interval=0.000085-0.000010),133(count=1,Time interval=0.000078-0.000000),135(count=2,Time interval=0.000087-0.000024),136(count=0, Time interval=0.000000-0.000000)
harry 134(count=0, Time interval),133(count=1, Time interval),135(count=0, Time interval),136(count=0, Time interval)
joanne 134(count=1, Time interval),133(count=3, Time interval),135(count=0, Time interval),136(count=1, Time interval)
[哈利和乔安的时间间隔的计算方法与史蒂夫相同]。
基本上,如果我采取史蒂夫,我必须计算每个史蒂夫133,134,135,136有多少条目,以及133,134,135,136在什么时间间隔发生。我希望你们得到我的疑问。
答案 0 :(得分:0)
这应该让你入门
>>> from collections import defaultdict
>>> names = defaultdict(list)
>>> with open('f.txt') as f:
... lines = [l.split() for l in f.readlines() if l.strip()]
... for l in lines:
... names[l[0]].append([l[1],l[2]])
>>> for i in names.iteritems():
... print i
...
('steve', [['134', '0.000010'], ['135', '0.000024'], ['133', '0.000078'], ['134',
'0.000085'], ['135', '0.000087']])
('joanne', [['134', '0.000022'], ['133', '0.000045'], ['133', '0.000079'], ['133', '0.000081'], ['136', '0.000083']])
('Harry', [['133', '0.000012']])