The string List in Python is sorted by length

  • 2021-07-06 11:25:38
  • OfStack

Let's look at the implementation of string List sorted by length (python)

myList = ['青海省','内蒙古自治区','西藏自治区','新疆维吾尔自治区','广西壮族自治区']

1. First, get the length of each string

2. Sort, select sorted or list. sort () for sorting

The built-in sorted returns a new list, while list. sort operates on the list

sorted(iterable, cmp=None, key=None, reverse=False)

iterable: Is an iterative type;
cmp: A function for comparison, which is determined by key;
key: Use a certain attribute or function of the list element as a keyword, have a default value, and iterate 1 item in the collection;
reverse: Collation. reverse = True descending or reverse = False ascending, with default values.

Return value: is a sorted iterative type, like iterable1.


myList = [' Qinghai Province ',' Inner Mongolia Autonomous Region ',' Tibet Autonomous Region ',' Xinjiang Uygur Autonomous Region ',' Guangxi Zhuang Autonomous Region '] 
myList1 = sorted(myList,key = lambda i:len(i),reverse=True) 
print(myList1) 
myList = [' Qinghai Province ',' Inner Mongolia Autonomous Region ',' Tibet Autonomous Region ',' Xinjiang Uygur Autonomous Region ',' Guangxi Zhuang Autonomous Region '] 
myList.sort(key = lambda i:len(i),reverse=True) 
print(myList)

The results are as follows:


[' Xinjiang Uygur Autonomous Region ', ' Guangxi Zhuang Autonomous Region ', ' Inner Mongolia Autonomous Region ', ' Tibet Autonomous Region ', ' Qinghai Province '] 

PS: Here's how the strings in the following list are sorted according to a certain rule (python)

Sometimes, when processing data, you want to sort according to the size of the numbers in the string.

For example, there are 1 set of record files, '1. dat', '2. dat'...

When I read all the record file names in this folder into a list, these strings are arranged as follows:

How do I arrange these strings numerically?

1. Firstly, the numbers in the string are extracted through regular expressions

2. Sort, select built-in function sorted for sorting

sorted(iterable, cmp=None, key=None, reverse=False)

iterable: Is an iterative type;
cmp: A function for comparison, which is determined by key;
key: Use an attribute or function of a list element as a keyword, have a default value, and iterate 1 item in the collection;
reverse: Collation. reverse = True descending or reverse = False ascending, with default values.
Return value: is a sorted iterative type, like iterable1.

Therefore, if the sorting mode is shot according to numbers, key should correspond to the numbers inside.

Therefore, this problem only needs one sentence to solve:


s = ['1.dat','10.dat','5.dat']
new = sorted(s,key = lambda i:int(re.match(r'(\d+)',i).group()))
print new

The result is

For this operation, you can also use list. sort (). After python 2.4, list. sort and sorted both add an key parameter to specify a function

The difference is that the built-in sorted returns a new list, while list. sort operates on the list


s = ['1.dat','10.dat','5.dat']
s.sort(key = lambda i:int(re.match(r'(\d+)',i).group()))
print s 

Summarize


Related articles: