Python中的垂直排序

2024-04-23 14:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个excel文件,现在需要排序。每样东西都用标签隔开,大约有3000行。我想它是按行排序,然后按框,如果行相等。数据如下所示:

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

任何指点都将不胜感激。你知道吗


Tags: 文件数据box排序clone标签descriptionexcel
1条回答
网友
1楼 · 发布于 2024-04-23 14:24:56

正如凯文所指出的,普通的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))

相关问题 更多 >