python list sorted by numeric character size

python list sorted by numeric character size

In the list csv written procedure, a list of scheduling problems encountered, the list is stored in a digital character, you need to sort by size digital
test source

testList = ['1', '5', '2', '10', '50', '21', '31', '3', '7']                                                                                                                                 
print('testList={}'.format(testList))                                                                                                                                                        
                                                                                                                                                                                             
normalSortList = testList[:]
normalSortList.sort()
print('after sort(): {}'.format(normalSortList))

intSortList = testList[:]
intSortList.sort(key = int)
print('after sort(key=int): {}'.format(intSortList))

Run
to python3 ./test.py
testlist = [ '. 1', '. 5', '2', '10', '50', '21 is', '31 is', '. 3', '. 7']
After Sort () : [ '1', '10', '2', '21', '3', '31', '5', '50', '7'] # will find here 2 after 10, obviously not themselves required
after sort (key = int): [ '1', '2', '3', '5', '7', '10', '21', '31', '50'] # use sort (key = int) to sort the results on the right

Author: handsome too afraid to go out

Guess you like

Origin blog.csdn.net/zmlovelx/article/details/94554406