简体   繁体   English

如何按字符串长度后跟字母顺序对列表进行排序?

[英]How to sort a list by length of string followed by alphabetical order?

Given a list of words, return a list with the same words in order of length (longest to shortest), the second sort criteria should be alphabetical.给定一个单词列表,按照长度(从最长到最短)的顺序返回一个包含相同单词的列表,第二个排序标准应该是按字母顺序排列的。 Hint: you need think of two functions.提示:你需要考虑两个函数。

This is what I have so far:这是我到目前为止所拥有的:

def bylength(word1,word2):
    return len(word2)-len(word1)

def sortlist(a):
    a.sort(cmp=bylength)
    return a

it sorts by length but I don't know how to apply the second criteria to this sort, which is by alphabetical descending.它按长度排序,但我不知道如何将第二个标准应用于这种排序,即按字母降序。

You can do it in two steps like this:您可以分两步完成,如下所示:

the_list.sort() # sorts normally by alphabetical order
the_list.sort(key=len, reverse=True) # sorts by descending length

Python's sort is stable, which means that sorting the list by length leaves the elements in alphabetical order when the length is equal. Python 的排序是稳定的,这意味着当长度相等时,按长度对列表进行排序会使元素按字母顺序排列。

You can also do it like this:你也可以这样做:

the_list.sort(key=lambda item: (-len(item), item))

Generally you never need cmp , it was even removed in Python3.通常你永远不需要cmp ,它甚至在 Python3 中被删除了。 key is much easier to use. key更容易使用。

n = ['aaa', 'bbb', 'ccc', 'dddd', 'dddl', 'yyyyy']

for i in reversed(sorted(n, key=len)):
    print i

yyyyy dddl dddd ccc bbb aaa yyyyy dddl dddd ccc bbb aaa

for i in sorted(n, key=len, reverse=True):
     print i

yyyyy dddd dddl aaa bbb ccc yyyyy dddd dddl aaa bbb ccc

-Sort your list by alpha order, then by length.

See the following exmple:

>>> coursesList = ["chemistry","physics","mathematics","art"]
>>> sorted(coursesList,key=len)
['art', 'physics', 'chemistry', 'mathematics']
>>> coursesList.append("mopsosa")
>>> sorted(coursesList,key=len)
['art', 'physics', 'mopsosa', 'chemistry', 'mathematics']
>>> coursesList.sort()
>>> sorted(coursesList,key=len)
['art', 'mopsosa', 'physics', 'chemistry', 'mathematics']

First sort by Alphabet and then sort by Length.首先按字母排序,然后按长度排序。

Here is a working example这是一个工作示例

mylist.sort()
mylist = sorted(mylist, key=len, reverse=False)

# Print the items on individual line
for i in mylist:
    print(i)

Although Jochen Ritzel said you don't need cmp, this is actually a great use case for it!虽然 Jochen Ritzel 说你不需要 cmp,但这实际上是它的一个很好的用例! Using cmp you can sort by length and then alphabetically at the same time in half the time sorting twice would take!使用 cmp 您可以按长度排序,然后同时按字母顺序排序,只需排序两次所需的一半时间!

def cmp_func(a, b):
    # sort by length and then alphabetically in lowercase
    if len(a) == len(b):
        return cmp(a, b)
    return cmp(len(a), len(b))

sorted_the_way_you_want = sorted(the_list, cmp=cmp_func)

Example:例子:

>>> the_list = ['B', 'BB', 'AA', 'A', 'Z', 'C', 'D']
>>> sorted(the_list, cmp=cmp_func)
['A', 'B', 'C', 'D', 'Z', 'AA', 'BB']

Note, if your list is a mix of upper and lower case replace cmp(a, b) with cmp(a.lower(), b.lower()) as python sorts 'a' > 'Z' .请注意,如果您的列表是大写和小写的混合,请将cmp(a, b)替换为cmp(a.lower(), b.lower()) ,因为 python 排序 'a' > 'Z'

In python3 you'd need to be sorting objects with __lt__ style comparison functions defined or functools.cmp_to_key() which does that for you.在 python3 中,您需要使用定义的__lt__样式比较函数或为您执行此操作的functools.cmp_to_key()对对象进行排序

def cmp_func(a, b): # sort by length and then alphabetically in lowercase if len(a) == len(b): return cmp(a, b) return cmp(len(a), len(b)) def cmp_func(a, b): # 按长度排序,然后按字母小写 if len(a) == len(b): return cmp(a, b) return cmp(len(a), len(b))

sorted_the_way_you_want = sorted(the_list, cmp=cmp_func) sorted_the_way_you_want = sorted(the_list, cmp=cmp_func)

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

相关问题 "如何按长度对列表进行排序,然后按字母倒序排列" - How to sort a list by length and then in reverse alphabetical order 首先根据长度对字符串元素列表进行排序,然后根据长度匹配的元素的字母顺序对其进行排序 - Order a list of string elements based on length first and then sort it based on alphabetical order for elements with matching length 如何在JSON文件中按字母顺序按一个字符串对列表进行排序 - How to sort a list by one string in alphabetical order in a JSON file 如何按数字顺序而不是字母顺序对列表中的元素进行排序 - how to sort the elements in a list in numerical order and not alphabetical 根据长度第一和字母顺序第二对列表中的元素进行排序 - Sort elements in list based on length first and alphabetical order second 如何在python中按字母顺序对列表进行排序? - how to sort list in alphabetical order in python? 按长度和字母顺序对字符串进行基数排序 - Radix Sort for Strings by length and alphabetical order 如何在Python中按字母顺序对字符串排序? - How do I sort a string in alphabetical order in Python? 如何在没有内置函数的情况下按字母顺序对字符串进行排序 - how to sort a string into alphabetical order without inbuilt functions 如何使用python以给定的字母顺序排序列表? - How to a sort list in the given alphabetical order using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM