立即学习:https://edu.csdn.net/course/play/26676/338786?utm_source=blogtoedu
range(4) // 0,1,2,3
range(2,4) // 2,3
list(range(100))
zip()
a = [1, 2, 3]
b = [4, 5, 6]
c = [1, 2, 3]
d = [4, 5, 6, 7]
list( zip(a, b) )
// [(1, 4), (2, 5), (3, 6)]
list(zip(c, d))
// [(1, 4), (2, 5), (3, 6)]
for x, y in zip(a, b):
print(x + y)
// 5 7 9
enumerate()
// 一般用于遍历中需要用到索引时
s = ['a', 'b', 'c', 'd']
list( enumerate(s) )
// [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
列表解析
[ i**2 for i in range(10) ]
[i for i in range(100) if i %3 == 0]
s = 'Life is short You need Python'
[len(word) for word in s.split(' ')]