Python list (list) method example, pythonlist
Preface
In daily development, some API interfaces limit the number of requested elements. In this case, you need to split a large list into a fixed small list and then perform related processing, this article has collected several simple methods and shared them for your reference. Let's take a look at the detailed introduction below:
Example
#1. split the large list into a small list of three elements. If not three elements are used as a list, output In [17]: lstOut [17]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [18]: for I in range (0, len (lst), 3 ):...: print lst [I: I + 3]...: [0, 1, 2] [3, 4, 5] [6, 7, 8] [9] #2. with a slight improvement, a list derivation is used, and the results are put into a list In [35]: lstOut [35]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [36]: B = [lst [I: I + 3] for I in range (0, len (lst), 3)] in [37]: bOut [37]: [0, 1, 2], [3, 4, 5], [6, 7, 8], [9] In [38]: #3. lambda, which I don't like very much, cannot be understood by Meng Yi. I will look at the logic In [10]: f = lambda a: map (lambda B: a [B: B + 3], range (0, len (a), 3) In [11]: lstOut [11]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [12]: f (lst) Out [12]: [0, 1, 2], [3, 4, 5], [6, 7, 8], [9]
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.