Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将“无”值推到末尾时对列表进行排序_Python_List_Sorting_Nonetype - Fatal编程技术网

Python 将“无”值推到末尾时对列表进行排序

Python 将“无”值推到末尾时对列表进行排序,python,list,sorting,nonetype,Python,List,Sorting,Nonetype,我有一个没有对象的同质对象列表,但它可以包含任何类型的值。 例如: >>> l = [1, 3, 2, 5, 4, None, 7] >>> sorted(l) [None, 1, 2, 3, 4, 5, 7] >>> sorted(l, reverse=True) [7, 5, 4, 3, 2, 1, None] 有没有一种方法不需要重新设计轮子,就可以像python那样对列表进行排序,但列表末尾没有值,如下所示: [1, 2, 3, 4

我有一个没有对象的同质对象列表,但它可以包含任何类型的值。 例如:

>>> l = [1, 3, 2, 5, 4, None, 7]
>>> sorted(l)
[None, 1, 2, 3, 4, 5, 7]
>>> sorted(l, reverse=True)
[7, 5, 4, 3, 2, 1, None]
有没有一种方法不需要重新设计轮子,就可以像python那样对列表进行排序,但列表末尾没有值,如下所示:

[1, 2, 3, 4, 5, 7, None]
我觉得这里有一些使用“key”参数的技巧

试试这个:

sorted(l, key=lambda x: float('inf') if x is None else x)
由于无穷大大于所有整数,
None
将始终放在最后

>>> l = [1, 3, 2, 5, 4, None, 7]
>>> sorted(l, key=lambda x: (x is None, x))
[1, 2, 3, 4, 5, 7, None]

这将为列表中的每个元素构造一个元组,如果值为
None
则元组为
(True,None)
,如果值为其他值,则元组为
(False,x)
(其中
x
为值)。由于元组是逐项排序的,这意味着所有非
None
元素将首先出现(因为
False
),然后按值排序。

我创建了一个函数,该函数扩展了Andrew Clark的答案和Tutudaju的注释

def sort(myList, reverse = False, sortNone = False):
    """Sorts a list that may or may not contain None.
    Special thanks to Andrew Clark and tutuDajuju for how to sort None on https://stackoverflow.com/questions/18411560/python-sort-list-with-none-at-the-end

    reverse (bool) - Determines if the list is sorted in ascending or descending order

    sortNone (bool) - Determines how None is sorted
        - If True: Will place None at the beginning of the list
        - If False: Will place None at the end of the list
        - If None: Will remove all instances of None from the list

    Example Input: sort([1, 3, 2, 5, 4, None, 7])
    Example Input: sort([1, 3, 2, 5, 4, None, 7], reverse = True)
    Example Input: sort([1, 3, 2, 5, 4, None, 7], reverse = True, sortNone = True)
    Example Input: sort([1, 3, 2, 5, 4, None, 7], sortNone = None)
    """

    return sorted(filter(lambda item: True if (sortNone != None) else (item != None), myList), 
        key = lambda item: (((item is None)     if (reverse) else (item is not None)) if (sortNone) else
                            ((item is not None) if (reverse) else (item is None)), item), 
        reverse = reverse)
以下是如何运行它的示例:

myList = [1, 3, 2, 5, 4, None, 7]
print(sort(myList))
print(sort(myList, reverse = True))
print(sort(myList, sortNone = True))
print(sort(myList, reverse = True, sortNone = True))
print(sort(myList, sortNone = None))
print(sort(myList, reverse = True, sortNone = None))

值是否保证为
int
s或
None
,或者是否需要
None
对所有任意对象进行排序?否,可以是string或None,也可以是float和None。一般来说,它是一个没有的同构列表elements@NikolayGolub:为了将来的参考,最好在问题中清楚地说明这一点毕竟SethMMorton的回答符合你的要求,但不符合你的实际需要,因此,如果F.J没有发生,你就不会有一个好的解决方案。@abarnert我用了第一句话,“我有一个没有的同质对象列表”,所以我假设OP的列表不是异质的。@abarnert我明白你的意思了。我想这可以追溯到几天前的XY讨论。我现在明白了为什么问“为什么”很重要了。我喜欢这个解决方案,因为它适用于所有类型的同类产品litst@NikolayGolub:它甚至支持异构列表(当然,只要您的异构类型一开始都是可比较的)。如果在此解决方案中使用
reverse=True
,使用键中的
x不是None
,否则首先返回所有None值。@tututudaju如果要按降序值排序且最后无值,则不需要将True和False放在第一位的元组。您可以简单地对
[7,5,4,3,2,1,None]
进行排序(l,reverse=True)
,如原问题所示。@tututudaju如果排序类似于datetimes,则是正确的。哇!我不知道这个“inf”太棒了!如果列表包含无穷大或列表是字符串列表,则此操作无效。@NeilG是的,我们在对问题本身的评论中已经介绍了这一点。这个答案是针对最初发布的问题,但仍然只适用于整数,所以我没有删除它。当你的函数变得这么大时,为什么不将它分解成一个合适的函数定义呢?把它留作lambda只会让人难以阅读。