- 很妙
def group(lst, n):
"""group([0,3,4,10,2,3], 2) => [(0,3), (4,10), (2,3)]
Group a list into consecutive n-tuples. Incomplete tuples are
discarded e.g.
>>> group(range(10), 3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
"""
return zip(*[lst[i::n] for i in range(n)])
-
lst[i::n]
的作用
lst = range(10)
n = 5
print(*[lst[i::n] for i in range(n)])
>> range(0, 10, 5) range(1, 10, 5) range(2, 10, 5) range(3, 10, 5) range(4, 10, 5)
网友评论