List Sort Python string length - python

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

The following look-implemented method List Sort string length (Python) of

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

1, the length of each string was first

2, sorting, or selecting the list.sort sorted () to sort

Built-sorted returns a new list, but the list is operated list.sort

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

iterable: Iteration is the type;
cmp: function used to compare to compare what is determined by the Key;
Key: perform as a keyword with an attribute or function list element, has a default value Iteration item in the collection;
Reverse: collation. reverse = True in descending or ascending order reverse = False, you have default values.

Returns: an iterator type can be sorted, and iterable same.

myList = [ 'Qinghai', 'Inner', 'Tibet', 'Xinjiang', 'Guangxi'] 
myList1 the sorted = (myList, the lambda I = Key: len (I), Reverse = True) 
Print (myList1) 
myList = [ 'Qinghai', 'Inner', 'Tibet', 'Xinjiang', 'Guangxi'] 
myList.sort (Key = the lambda I: len (I), = Reverse True) 
Print (myList)

The results are as follows:

[ 'Xinjiang', 'Guangxi', 'Inner', 'Tibet', 'Qinghai'] 

PS: The following list of strings look sort method according to certain rules (Python)

Sometimes processing data, sorted by size desired string of numbers.

For example, there is a set of log files, respectively '1.dat', '2.dat' to ...

When I put all the records in the file name in the folder read a list, the arrangement of these strings is as follows:

How to make these strings arranged in digital?

1, the first regular expressions, extracts a digital string

2, sort, select built-in function to sort sorted

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

iterable: Iteration is the type;
cmp: function used to compare to compare what is determined by the Key;
Key: perform as a keyword with an attribute or function list element, has a default value Iteration item in the collection;
Reverse: collation. reverse = True in descending or ascending order reverse = False, you have default values.
Returns: an iterator type can be sorted, and iterable same.

So Sort according to digital shoot, then, key figures inside must correspond.

So, to solve this problem just one sentence:

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

 The result of that is,

 For this operation, you can also use the list.sort (), after python2.4, list.sort are sorted and added to a key parameter used to specify a function

The difference is: a built-sorted returns a new list, but the list is list.sort operation

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

to sum up

The above is a small series to introduce Python string List in accordance with the length of the sort, we want to help, if you have any questions please give me a message, Xiao Bian will promptly reply to everyone. Here also very grateful to everyone for their support of the school network Hi!
If you think this article helpful to you, welcome to reprint, please indicate the source, thank you!

The original address is: http: //www.piaodoo.com/thread-14067-1-51.html Crazy Love www.eplbx.com 131 www.buzc.org stockings control www.txdah.com to learning can be enjoyable help to learn better! Despite the very good study may help to better learning enjoyable! very good!

Guess you like

Origin www.cnblogs.com/txdah/p/12128481.html