Python语法简洁,能够用一行代码实现很多有趣的功能,这次来整理30个常见的Python一行代码集合。
1、转置矩阵
old_list = [[1, 2, 3], [3, 4, 6], [5, 6, 7]]
list(list(x) for x in zip(*old_list))
[[1, 3, 5], [2, 4, 6], [3, 6, 7]]
2、二进制转十进制
decimal = int('1010', 2)
print(decimal) #10
10
3、字符串大写转小写
# 方法一 lower()
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
# 方法二 casefold()
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'
'hi my name is allwin'
4、字符串小写转大写
"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'
'HI MY NAME IS ALLWIN'
5、将字符串转换为字节
"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode metho