Python 常用技巧
if else 语句:
表达式 c = a if a > b else b:
1 >>>a = 1 2 >>>b = 2 3 >>>c = a if a > b else b 4 #2
二维列表:
c = [b,a][a>b]
1 >>>a = 1 2 >>>b = 2 3 >>>c = [b,a][a>b] 4 #2
c = (a > b and [a] or [b])[0]
1 >>>a = 1 2 >>>b = 2 3 >>>c = (a > b and [a] or [b])[0] 4 #2
输入n个数转换为list:
1 li = list(map(int, input().strip().split()))
a//b
>>> 49//2 #24
反转:str[::-1]
>>>a = '1234' >>>a[::-1] #4321