标签:杂记 lis 运算符 style append bsp pytho end 学习
常用三元运算符来简化分支和循环的代码:
分支简化:
a = 1
b = 2
c = a if a > 1 else b
print(c)
# 以上几行等同于
a = 1
b = 2
if a >1:
c = a
else:
c = b
print(c)
循环简化:
nums = list(range(1,11))
s = [i for i in nums if i % 2 == 0]
print(s)
# 以上代码等同于
s = []
nums = list(range(1,11))
for i in nums:
if i % 2 == 0:
s.append(i)
print(s)
标签:杂记 lis 运算符 style append bsp pytho end 学习
原文地址:http://www.cnblogs.com/houzhizhe/p/6946166.html