对值进行排序并将最佳分数(最高值)变灰

2024-05-28 18:35:48 发布

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

我刚刚开始学习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选择on。你知道吗

2)现在当我有唯一的值时,我想根据score:之后的数字对它们进行排序,这意味着最负的数字在第一个位置,而最正的数字在最后一个位置。你知道吗


Tags: 文件id技巧排序情况数字解决方案编号
3条回答

这是一种可能性:

import pprint

scores = {}
for line in open('/tmp/data.txt'):
    _, code, _, score, _, _, _, _, _, _ = line.split()
    if code not in scores:
        scores[code] = score

pprint.pprint(scores)

sorted_by_score = sorted(
    [(code, score) for code, score in scores.items()],
    key=lambda v: v[1],
    reverse=True)

pprint.pprint(sorted_by_score)

元组列表可以用于第一部分,但速度较慢。你知道吗

您可以使用简单的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]))

嗨,这里有一个可能的解决方案

    def readLine(line,acc):
      result =line.split()
      id=result[1]
      value=result[3]
      if id not in acc:
            acc[id]=value;

    def main():
      filepath = 'myfile.csv'
      acc={};
      with open(filepath) as fp:
            for line in fp:
            readLine(line,acc)
      for key, value in sorted(acc.iteritems(), key=lambda (k, v): (v, k)):
            print "%s: %s" % (key, value)

    if __name__ == "__main__":
      main()

相关问题 更多 >

    热门问题