测试源码
点击(此处)折叠或打开
- 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))
python3 ./test.py
testList=[‘1’, ‘5’, ‘2’, ‘10’, ‘50’, ‘21’, ‘31’, ‘3’, ‘7’]
after sort(): [‘1’, ‘10’, ‘2’, ‘21’, ‘3’, ‘31’, ‘5’, ‘50’, ‘7’] #会发现这里2在10之后,显然不是自己需要的
after sort(key=int): [‘1’, ‘2’, ‘3’, ‘5’, ‘7’, ‘10’, ‘21’, ‘31’, ‘50’] #使用sort(key=int)来排序,结果就对了
作者:帅得不敢出门