Python list (list) method example, pythonlist

Source: Internet
Author: User
Tags python list

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.