How python uses zip to group adjacent list items
This article mainly shows you "how python uses zip to group adjacent list items". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how python uses zip to group adjacent list items".
Use zip to group adjacent list items
> a = [1,2,3,4,5,6] > > # Using iterators > > group_adjacent = lambda a, k: zip (* ([iter (a)] * k)) > group_adjacent (a, 3) [(1,2,3), (4,5,6)] > > group_adjacent (a, 2) [(1,2), (3,4), (5,6)] > > group_adjacent (a, 1) [(1,), (2,), (3,), (4) ), (5,), (6,)] > > # Using slices > > from itertools import islice > > group_adjacent = lambda a, k: zip (* (islice (a, I, None, k) for i in range (k) > group_adjacent (a, 3) [(1,2,3), (4,5,6)] > group_adjacent (a, 2) [(1,2), (3,4), (5,6)] > > group_adjacent (a, 1) [(1,), (2) ), (3,), (4,), (5,), (6,)] these are all the contents of the article "how python uses zip to group adjacent list items" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!