没有排序功能的情况下,按某个元素对嵌套列表进行排序. [英] Sorting a nested list by a certain element without the sorting function.

查看:78
本文介绍了没有排序功能的情况下,按某个元素对嵌套列表进行排序.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的嵌套列表:

If I have a nested list like this:

L = [['James', '1', '2'], ['Alan', '1', '1'], ['Henry', '1', '5']]

如何在不使用排序或排序功能的情况下,根据每个子列表中的最后一个数字从最高到最低对它进行排序?

How can I sort it from highest to lowest based on the last number in each of the sub lists without using the sorting or sorted function?

输出:

final = [['Henry', '1', '5'], ['James', '1', '2'], ['Alan', '1', '1']]

我知道如何执行此操作的唯一方法是使用排序功能,但是我想知道如何执行此操作.

The only way I know how to do it is with the sorting function, but I would like to know how to do it without that.

推荐答案

您可以编写自己的 quicksort 函数.您几乎可以切换一些符号来反转输出:

You can write your own quicksort function. You can pretty much switch some signs to reverse the output:

def quicksort(arr):
    if len(arr) <= 1:
        return arr

    pivot = arr[len(arr)/2]

    left = [x for x in arr if x > pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x < pivot]

    return quicksort(left) + middle + quicksort(right)

下一步是确保提取正确的数字,并确保可以将其映射回父列表,以便可以将其再次放到正确的位置-

The next step would be to make sure you're extracting the proper numbers, and make sure you can map those back to the parent list so you can drop those into the right place again — Allen's answer has a good approach:

L = [['James', '1', '2'], ['Alan', '1', '1'], ['Henry', '1', '5']]

keys = [(int(v[-1]),k) for k,v in enumerate(L)] # This line here

该行基本上会创建一个元组列表,其中存储了我们要排序的数字,以及该数字的父级列表在总体列表中的索引.所以基本上,对于Lkeys = [(2, 0), (1, 1), (5, 2)].

That line basically creates a list of tuples that store the numbers we wanna sort by, and the index of that number's parent list on the overarching list. So basically, for L, keys = [(2, 0), (1, 1), (5, 2)].

因此,您可以通过创建可以递归使用的子功能来更改quicksort函数以解决所有这些问题:

So you'd alter the quicksort function to account for all this, by creating a subfunction that can be used recursively:

def quicksort(arr):
    # Put the actual sorting function into a subfunction that can be called recursively
    def subsort(arr2):
        if len(arr2) <= 1:
            return arr2
        pivot = arr2[len(arr2)/2]
        left = [x for x in arr2 if x > pivot]
        middle = [x for x in arr2 if x == pivot]
        right = [x for x in arr2 if x < pivot]
        return subsort(left) + middle + subsort(right)

    # Get the value-key pairs and sort them
    keys = [(int(v[-1]),k) for k,v in enumerate(L)]
    keys = subsort(keys)

    # Do the mapping back to the original array fed into the main function
    final = []
    for i in keys:
        final.append(arr[i[1]])

    return final

并以此:

>>> L = [['James', '1', '2'], ['Alan', '1', '1'], ['Henry', '1', '5']]
>>> quicksort(L)
[['Henry', '1', '5'], ['James', '1', '2'], ['Alan', '1', '1']]

注意:如果在最后一个位置有两个编号相同的项目,则它们将在原始列表中得到它们的原始相对位置(彼此).已还原 .有关元组比较的更多详细信息,请参见此答案,并请注意,我们正在对降序进行排序>在此排序(否则,他们将保持彼此相对的原始位置).所以:

Note: If there are two items with the same number in the last position those are gonna get their original relative position (to one another) in the original list reverted. See this answer for more details on tuple comparison, and note that we're sorting stuff in descending order here (otherwise, they'd just keep their original position relative to one another). So:

>>> L = [['Simon', '1', '2'], ['Henry', '1', '5'], ['Finn', '1', '2']
>>> quicksort(L)
[['Henry', '1', '5'], ['Finn', '1', '2'], ['Simon', '1', '2']]

这篇关于没有排序功能的情况下,按某个元素对嵌套列表进行排序.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆