简体   繁体   中英

May I generate permutations of a long list for calculation without creating a huge list (in Python)?

I am currently having a project require me to find out specific permutations of a list of integers which fits certain conditions. I have tried itertools yet it generates a list which is too huge to my RAM which makes me can not even tried to put it in further calculations. I want to know if there is someway I can generate those permutations one by one and calculate them individually without having my computer loading a huge lists of data.

itertools.permutations , just like most of the other functions related to collections in the built-in modules, returns a Python iterator .

In a nutshell, it means that each element of the resulting collection is returned one by one (or, in Python's terms, it is yield ). The result is not automatically stored as a whole.

When you call permutations , you can either:

  1. Instantiate an actual list or whatever collection with all the values at once.
    Example:

     seq = permutations(range(100), 4) list(seq) # beware, 94,109,400 elements, around 1.5GB of storage
  2. Iterate on each value and decide to keep or discard each of them. You decide what is stored in memory.
    Example:

     seq = permutations(range(100), 4) predicate = lambda a, b, c, d: 50 < a == 25*d > b*2 < 4 < 3*c <= 15 >= 5*d list(p for p in seq if predicate(*p)) # [(75,0,2,3), (75,0,4,3), (75,0,5,3), (75,1,2,3), (75,1,4,3), (75,1,5,3)]

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