2 回答
TA贡献1796条经验 获得超4个赞
做这个:
from collections import defaultdict
with open('1.txt') as fin:
frame_rects = defaultdict(list)
for row in (map(int, line.split()) for line in fin):
id, frame, rect = row[0], row[2], row[3:7]
frame_rects[frame].append((id, rect))
# print '{} {} {}'.format(frame, id, rect) # (if you want to sample)
for key, value in frame_rects.items():
print key, ' '.join([' '.join([str(i) for i in v]) for v in value])
输出:
1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36]
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39]
TA贡献1824条经验 获得超8个赞
from collections import defaultdict
with open('abc') as f:
dic = defaultdict(list)
for line in f:
idx, lenght, frame, X, Y, W, H, _ = map(int, line.split())
dic[frame].append([idx, [X, Y, W, H] ])
print dic
print "Expected output:"
for k, v in dic.items():
print "{} {}".format(k, "".join(["{} {} ".format(*lis) for lis in v ]) )
输出:
defaultdict(<type 'list'>,
{1: [[2, [561, 1, 20, 28]], [5, [553, 197, 20, 20]], [6, [113, 393, 36, 36]]],
2: [[2, [557, 1, 24, 32]], [5, [552, 197, 23, 21]], [6, [113, 391, 39, 39]]]})
Expected output:
1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36]
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39]
添加回答
举报