简体   繁体   中英

Python - Sort a list of lists but only keep last item in each sublist

I have a list of items that I need to sort according to a given score I give it. The code looks something like this:

original_list = [item_1, item_2, item_3]
# ... Here I score the list in a separate function to get a score for each item ...
scored_list = [[35, item_1], [45, item_2], [1, item_3]]
scored_list.sort()
sorted_list = [item[1] for item in scored_list]

So I have a list of items, score each item, sort the list according to the score, and then remove the score variable to keep only the items.

Is this the most efficient way of doing this kind of operation, or are there simpler ways to obtain the same result?

Efficiency-wise I doubt you can do better. If you are concerned with the number of lines, you can move the sort to inside the comprehension:

scored_list = [[35, item_1], [45, item_2], [1, item_3]]
sorted_list = [item[1] for item in sorted(scored_list)]

But in terms of speed you'll likely not notice a difference. In fact this may be a bit slower than your approach because in-place sort() might be a bit faster as it does not need to make a copy but depending on your data that is negligible.

So I'd say your approach is perfectly fine. If you want to make it more concise, you can use sorted() .

That's the right way, but you can use list comprehensions:

scored_list = [i[1] for i in sorted(scored_list, key=lambda s: s[0])]

For disentangling the scores and the items, you can use the:

the_scores, the_items = zip(*scored_list)

For efficiency, there are not much that you can do. Actually, I think that there is not something else that you can do.

You could use the key parameter in the sorted() function when you sort the array. You can pass in a lambda function that returns a list of scores using your scoring function as the key.

    def calcScore(x):
       # Calculate score here
       pass
    original_list = [item_1, item_2, item_3].
    sorted_list = sorted(original_list, key=lambda x: [calcScore(x) for x in original_list])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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