截取字符串
1 2 3
| str = "ABCDEFGFEE03" print (str[10:12])
|
分割字符串取倒数第一个
1 2 3
| str = "HELLO6CF51D000003" print(str.split('0000')[-1])
|
列表中的字符串排序
语法:list.sort(key=None, reverse=False) ,【参考】
Int型排序
1 2 3 4
| a = [5, 2, 3, 1, 4] a.sort() print(a)
|
字符默认排序
1 2 3 4 5 6
| import re list = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook']; list.sort() print(list)
|
表达式排序
1 2 3 4 5 6
| import re list = ['ch1.txt', 'ch3.txt', 'ch9.txt', 'ch10.txt', 'ch11.txt'] list.sort(key = lambda x:int(re.match('\D+(\d+)\.txt',x).group(1))) print(list)
|
自定义排序
按照字符尾数数字排序
1 2 3 4 5 6 7
| list = ["HEADF84300000A","HEAD97AB000009","HEADF51D000003","HEAD7952000002"]
list.sort(key=lambda x:int(x.split('0000')[-1],16)) print(list)
|