Python split list

Source: Internet
Author: User

For a large list, such as a list of more than 10,000 elements, if you need to make a complex and time-consuming calculation of each element in the list, it is very slow to use single-threaded processing, it is necessary to use multi-threaded processing, before processing the first need to split the large list, split into a small list, Here's a way to write a split list of your own:

Each of these is the size of each list, and Len (LS)/eachexact avoids rounding down evenly, resulting in a reduction in the total number of groupings.

Note: The number of groupings is not a simple len (ls)/each +1, because it is possible to divide exactly, without the remainder.

1     defDivide (Ls,each):2dividedls=[]3eachexact=float (each)4Groupcount=len (LS)/ each5Groupcountexact=len (LS)/eachexact6start=07          forIinchxrange (groupcount):8Dividedls.append (ls[start:start+Each ])9start=start+ eachTen         ifGroupcount<groupcountexact:#if the remainder, add all the remaining elements to the last group OneDividedls.append (ls[groupcount*Each :]) A         returnDividedls

Python split list

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.