Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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
Python3排序列表->所有以小写开头的条目_Python_List_Sorting - Fatal编程技术网

Python3排序列表->所有以小写开头的条目

Python3排序列表->所有以小写开头的条目,python,list,sorting,Python,List,Sorting,结果应该是 l1 = ['B','c','aA','b','Aa','C','A','a'] 与l1.sort相同,但以所有以小写字母开头的单词开头。尝试以下方法: ['a','aA','b','c','A','Aa','B','C'] 编辑: 对于喜欢命令式方法的人,使用list.sort方法的一行程序: >>> l = ['B', 'b','a','A', 'aA', 'Aa','C', 'c'] >>> sorted(l, key=str.swapc

结果应该是

l1 = ['B','c','aA','b','Aa','C','A','a']
与l1.sort相同,但以所有以小写字母开头的单词开头。

尝试以下方法:

['a','aA','b','c','A','Aa','B','C']
编辑: 对于喜欢命令式方法的人,使用list.sort方法的一行程序:

>>> l = ['B', 'b','a','A', 'aA', 'Aa','C', 'c']
>>> sorted(l, key=str.swapcase)
['a', 'aA', 'b', 'c', 'A', 'Aa', 'B', 'C']
注:
第一种方法保持l的状态不变,而第二种方法确实改变了l的状态。

以下是您可能需要的:

>>> l.sort(key=str.swapcase)
>>> print l
['a', 'aA', 'b', 'c', 'A', 'Aa', 'B', 'C']

请看一下这些文件。它说,在排序算法中进行比较时,可以提供自己的函数来检索列表的元素。输出列表中的元素比输入列表中的元素多。aA和aA来自什么?它们在l1中不存在。你的分类标准是什么?很好,比我的好得多。这就是我要找的。请您添加选项l.sortkey=str.swapcase好吗
    li = ['a', 'A', 'b', 'B']

    def sort_low_case_first(li):
        li.sort()  # will sort the list, uppercase first
        index = 0  # where the list needs to be cuted off
        for i, x in enumerate(li):  # iterate over the list
            if x[0].islower():  # if we uncounter a string starting with a lowercase
                index = i  # memorize where
                break  # stop searching
        return li[index:]+li[:index]  # return the end of the list, containing the sorted lower case starting strings, then the sorted uppercase starting strings 

    sorted_li = sort_low_case_first(li)  # run the function
    print(sorted_li)  # check the result
    >>>  ['a', 'b', 'A', 'B']