Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the Compressor of adjacent elements in list by python

Shulou Source: shulou.com Published: 2022-06-01 05:43:06 09月22日 Update

Editor to share with you how python to achieve the list of adjacent element compressor, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

List adjacent element Compressor

> a = [1,2,3,4,5,6] > zip (* ([iter (a)] * 2)) [(1,2), (3,4), (5,6)] > > 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) > > group_adjacent (a, 1) [(1,), (2,), (3,), (4,), (5,), (6,)] > > zip (a [:: 2], a [1zip 2]) [(1, 2), (3, 4), (5, 6)] > > zip (a [: 3], a [1: 3], a [2])) [(1,2,3), (4,5)] > group_adjacent = lambda a, k: zip (* (a [ifor i in range 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 to implement a list adjacent element Compressor by python" 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: Elements compressors articles content not much most more knowledge industry information information channels channels references learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei Shulou Technology Apple MySQL