Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses zip to group adjacent list items

Shulou Source: shulou.com Published: 2022-06-01 04:49:40 10月02日 Update

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!

Tags: Grouping content articles learning help easy to understand more organization knowledge editing industry information information channels channels research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Redmi MySQL Docker