-2

我有一个曾经是现在需要排序的 excel 文件。一切都由制表符分隔,大约有 3000 行。如果行相等,我希望它按 Row 排序,然后按 Box 排序。数据如下:

Row Box Description
17  3   C. trach clone
14  6   OMP A E Coli
1   6   R 19 15
2   5   11 Black Ring
1   1   L. Pneumo

我希望它阅读

Row Box Description
1   1   L. Pneumo
1   6   R 19 15
2   5   11 Black Ring
14  6   OMP A E Coli
17  3   C. trach clone

任何指针将不胜感激。

4

1 回答 1

1

正如凯文所指出的,普通sort就可以了:

#! /usr/bin/python3.2

#here you should actually use the csv module for parsing your data
#make sure that there are actually tabs between the columns
a = '''Row Box Description
17\t3\tC. trach clone
14\t6\tOMP A E Coli
1\t6\tR 19 15
2\t5\t11 Black Ring
1\t1\tL. Pneumo'''
data = [x.split('\t') for x in a.split('\n')[1:]]

data = sorted(data, key = lambda x: (int(x[0]), int(x[1])))
print('\n'.join('\t'.join(row) for row in data))
于 2013-11-06T18:45:41.100 回答