我刚刚开始学习 Python,如果有人愿意提供帮助,我需要一些帮助、技巧或解决方案。我有一个看起来像这样的文件: 2 C00000002 score: -48.649 nathvy = 49 nconfs = 3878 3 C00000001 score: -44.988 nathvy = 41 nconfs = 1988 4 C00000002 score: -42.674 nathvy = 49 nconfs = 6740 5 C00000002 score: -42.453 nathvy = 49 nconfs = 4553 6 C00000002 score: -41.829 nathvy = 49 nconfs = 7559 7 C00000002 score: -41.156 nathvy = 49 nconfs = 2251 8 C00000002 score: -39.520 nathvy = 49 nconfs = 3129 9 C00000004 score: -38.928 nathvy = 24 nconfs = 150 10 C00000002 score: -38.454 nathvy = 49 nconfs = 9473 11 C00000004 score: -37.704 nathvy = 24 nconfs = 156 12 C00000001 score: -37.558 nathvy = 41 nconfs = 51我的第二列是一些这里没有排序的ID,其中一些是重复的,例如(C00000001)。它们都分配了不同的编号,后跟score:(编号通常以 开头-)。我想做的是:1)阅读第二列(未排序的 ID)并始终选择出现的第一列。所以在这种情况下,C00000001它会选择与score : -44.988.2)现在当我有唯一值时,我想根据 之后的数字对它们进行排序score:,这意味着最负的数字位于第一个位置,而最正的数字位于最后一个位置。
1 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
你可以使用简单的python来做到这一点。Python 列表内置了排序方法
with open("in_file") as handle:
already_present = set()
l = []
for line in handle:
line_parts = line.strip().split()
l.append(line_parts)
key = line_parts[1]
if key in already_present:
continue
already_present.add(key)
l.sort(key=lambda x:float(x[3]))
添加回答
举报
0/150
提交
取消