Several Methods for splitting a list in Python

Source: Internet
Author: User
1.

zip(*(iter(range(10)),) * 3)
Or
The difference between izip_longest (* (ITER (range (10),) * 3) Is that izip_longest places the last group that does not satisfy the group in the last element.
 

2. L = range (10) [L [I: I + N] For I in range (0, Len (L), n)] # split the list by N
3:for key, group in itertools.groupby(range(10), lambda k: k//3):
     print key, list(group)
Grouping based on conditions that the key value meets
4:map
map(None, *[iter(range(10))] *3)
* 3 repeats the object three times.
 
 
Use asterisks * or ** when calling a function **

Test (* ARGs): * transmits each element in the ARGs sequence as a location parameter. If ARGs is equal to (1, 2, 3), the code is equivalent to test (1, 2, 3 ).

Test (** kwargs): ** converts the dictionary kwargs into a keyword parameter for transmission. If kwargs equals {'A': 1, 'B': 2, 'C': 3}, this code is equivalent to test (a = 1, B = 2, C = 3 ). * [Iter (range (10)] * 3 here is equivalent to <listiterator object at 0x17d76d0>, <listiterator object at 0x17d76d0>, <listiterator object at 0x17d76d0>, remove the list, send three values directly to map.
Map (none, * [iter (range (10)] * 3) is equivalent to map (none, <listiterator object at 0x17d76d0>, <listiterator object at 0x17d76d0>, <listiterator object at 0x17d76d0>)

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.