咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

Python测试代码

截取字符串

1
2
3
str = "ABCDEFGFEE03"
print (str[10:12])
# 输出:03

分割字符串取倒数第一个

1
2
3
str = "HELLO6CF51D000003"
print(str.split('0000')[-1])
# 输出:03

列表中的字符串排序

语法: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]

字符默认排序

1
2
3
4
5
6
import re
list = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook'];
list.sort()
print(list)

# 输出:['123', 'Facebook', 'Google', 'Runoob', 'Taobao']

表达式排序

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)

# 输出:['ch1.txt', 'ch3.txt', 'ch9.txt', 'ch10.txt', 'ch11.txt']

自定义排序

按照字符尾数数字排序

1
2
3
4
5
6
7
list = ["HEADF84300000A","HEAD97AB000009","HEADF51D000003","HEAD7952000002"]

# 方法一: 按照截取位置最后6位字符转 Int 16进制来排序
# list.sort(key=lambda x:int(x[8:14],16))
# 方法二: 按照'0000'分割,取最后2位转 Int 16进制来排序
list.sort(key=lambda x:int(x.split('0000')[-1],16))
print(list)