简体   繁体   中英

How to slice process itertools.product in python?

I want to process a very large itertools.product object. The issue is something like this:

import string
from itertools import product
text = string.lowercase[:] + string.uppercase[:] + '0123456789'
items = product(text, repeat=5)
for item in items:
    #do something

I know the items 's length is 62**5 . If I want to process the elements of items whose indices range from 300000 to 600000 , how to achieve this?

I have tried to convert the itertools.product to python list, like this:

items = list(product(text, repeat=5))[300000:600000+1]
for item in items:
    #do something

but it seems the conversion has consumed a large amount of memory since I have been waiting for a long time for this convert, and finally gave it up.

I have this demand because I want to do this thing in python gevent, so I want to slice the large itertool.product to small items for gevent spawn.

You can use islice to do this.

from itertools import product, islice
import string 

text = string.ascii_letters + string.digits
for item in islice(product(text,repeat=5), 300000, 600000):
    # do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM