C的逻辑思维通常是这么写的:
numbers = range(10)
evens = []
i = 0
while i < size :
if i % 2 == 0:
evens.append(i)
i += 1
evens
[0,2,4,6,8]
而用Python的逻辑思维是这么写的:
[ i for i in range(10) if i % 2 == 0 ]
[0,2,4,6,8]
大家明白为什么学Python了吧 !
C的逻辑思维通常是这么写的:
numbers = range(10)
evens = []
i = 0
while i < size :
if i % 2 == 0:
evens.append(i)
i += 1
evens
[0,2,4,6,8]
而用Python的逻辑思维是这么写的:
[ i for i in range(10) if i % 2 == 0 ]
[0,2,4,6,8]
大家明白为什么学Python了吧 !
转载于:https://www.cnblogs.com/xiaoyichong/p/10918136.html