- a = []
- for i in range(1,11):
- a.append(i)
- print(a) #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- #第一个2代表每2个元素组成一个小列表,第二个2表示组成小列表步长为2
- b = [a[i:i+2] for i in range(0,len(a),2)]
- print(b) #[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
- b = [a[i:i+2] for i in range(0,len(a),3)]
- print(b) #[[1, 2], [4, 5], [7, 8], [10]]
- b = [a[i:i+3] for i in range(0,len(a),2)]
- print(b) #[[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]
复制代码
|