简体   繁体   English

在python中排序列表

[英]sorting a list in python

I am trying to sort lists on a list : each list contains [seq1,seq2,score] , I want to sort the list L according to the score of (seq1,seq2) from the max score to the minimum score, then each list take a rank to each (seq1,seq2) according to the score 我正在尝试对列表中的列表进行排序:每个列表包含[seq1,seq2,score] ,我想根据(seq1,seq2)的得分从最大得分到最小得分对列表L进行排序,然后每个列表根据分数对每个(seq1,seq2)进行排名

L=[ ['AA', 'CG', 0],['AA', 'AA', 4], ['CG', '--', -1]]

the sorted list must be: 排序的列表必须是:

L=[['AA', 'AA', 4], ['AA', 'CG', 0], ['CG', '--', -1]]

for the ranks: 排名:

['AA', 'AA', 4] has rank 1
['AA', 'CG', 0] has rank 2
['CG', '--', -1] has rank 3

how can I do it? 我该怎么做? I tried: 我试过了:

def getKey():/* to get the score from each list*/
    scorelist=score()/*this is the list L*/
    return scorelist[][2]



def sort_list():
   scorelist=score()
    p=sorted(s, key=getKey)
    return p

You can use itemgetter as a function to get the value to sort the list by. 您可以将itemgetter用作函数来获取值以对列表进行排序。 Then to get the rank, you can use enumerate . 然后要获得排名,可以使用enumerate

from operator import itemgetter

score_list = score()
score_list.sort(key=itemgetter(2), reversed=True) # sort list descending by third element
for i, values in enumerate(score_list):
    print(values, i+1) # print values and rank

If several items can share the same score, you can use groupby to assign them the same rank: 如果多个项目可以共享相同的分数,则可以使用groupby为它们分配相同的排名:

from operator import itemgetter
from itertools import groupby

score_list = score()
score_list.sort(key=itemgetter(2), reversed=True)
for i, group in enumerate(groupby(score_list)):
    for item in group:
        print(item, i+1)

With this code, if you have 2 items ranked first, the next item will be ranked second. 使用此代码,如果您有2个项目排名第一,则下一个项目将排名第二。 If you want to rank it third (as expected), you can increment the rank in the inner loop (and save it rank for display): 如果要使其排名第三(按预期),则可以在内循环中增加排名(并保存其排名以供显示):

from operator import itemgetter
from itertools import groupby

score_list = score()
score_list.sort(key=itemgetter(2), reversed=True)
rank = 1 # counter for the rank
for group in groupby(score_list):
    current_rank = rank # save the rank
    for item in group:
        print(item, current_rank)
        rank += 1 # rank increment in inner loop

You're right on track with using the sorted function. 使用sorted功能使您走上正轨。 The key can be an itemgetter which yields the third list element. key可以是产生第三个列表元素的itemgetter In that case, you'll need to reverse the generated list to get the desired order: 在这种情况下,您需要反转生成的列表才能获得所需的顺序:

>>> import operator
>>> sorted(L, key=operator.itemgetter(2), reverse=True)
[['AA', 'AA', 4], ['AA', 'CG', 0], ['CG', '--', -1]]

Try this: 尝试这个:

for result in L:
   result.reverse()
L.sort()
for result in L:
   result.reverse()
L.reverse()

In this code you set the number of any list on the first place and sort it with the sort function. 在此代码中,首先设置任何列表的编号,然后使用sort函数对其进行排序。 After you have a sorted list from the smallest to the highest number. 从最小到最大的排序列表之后。 So you have to use the reverse function again so that you can use it for your ranked system 因此,您必须再次使用反向功能,才能将其用于排名系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM