简体   繁体   English

限制python中组合/排列的数量

[英]Limiting the number of combinations /permutations in python

I was going to generate some combination using the itertools, when i realized that as the number of elements increase the time taken will increase exponentially. 我将使用itertools生成一些组合,当我意识到随着元素数量的增加,所花费的时间将呈指数增长。 Can i limit or indicate the maximum number of permutations to be produced so that itertools would stop after that limit is reached. 我可以限制或指示要生成的最大排列数,以便itertools在达到该限制后停止。

What i mean to say is: 我的意思是:

Currently i have 目前我有

#big_list is a list of lists
permutation_list = list(itertools.product(*big_list))

Currently this permutation list has over 6 Million permutations. 目前,这种排列列表有超过600万个排列。 I am pretty sure if i add another list, this number would hit the billion mark. 我很确定如果我添加另一个列表,这个数字将达到十亿大关。

What i really need is a significant amount of permutations (lets say 5000). 我真正需要的是大量的排列(比方说5000)。 Is there a way to limit the size of the permutation_list that is produced? 有没有办法限制生成的permutation_list的大小?

You need to use itertools.islice , like this 你需要使用itertools.islice ,就像这样

itertools.islice(itertools.product(*big_list), 5000)

It doesn't create the entire list in memory, but it returns an iterator which consumes the actual iterable lazily. 它不会在内存中创建整个列表,但会返回一个迭代器,它会懒惰地使用实际的迭代。 You can convert that to a list like this 您可以将其转换为这样的列表

list(itertools.islice(itertools.product(*big_list), 5000))

itertools.islice has many benefits such as ability to set start and step . itertools.islice有许多好处,例如设置startstep能力。 Solutions below aren't that flexible and you should use them only if start is 0 and step is 1. On the other hand, they don't require any imports. 下面的解决方案并不灵活,只有当start为0且step为1时才应使用它们。另一方面,它们不需要任何导入。


You could create a tiny wrapper around itertools.product 你可以在itertools.product周围创建一个小包装器

it = itertools.product(*big_list)
pg = (next(it) for _ in range(5000)) # generator expression

(next(it) for _ in range(5000)) returns a generator not capable of producing more than 5000 values. (next(it) for _ in range(5000))返回一个不能产生超过5000个值的生成器。 Convert it to list by using the list constructor 使用list构造函数将其转换为list

pl = list(pg)

or by wrapping the generator expression with square brackets (instead of round ones) 或者用方括号(而不是圆形括号)包装生成器表达式

pl = [next(it) for _ in range(5000)] # list comprehension

Another solution, which is just as efficient as the first one, is 另一种解决方案与第一种解决方案一样有效

pg = (p for p, _ in zip(itertools.product(*big_list), range(5000))

Works in Python 3+, where zip returns an iterator that stops when the shortest iterable is exhausted. 在Python 3+中工作,其中zip返回一个迭代器,当最短的iterable耗尽时它停止。 Conversion to list is done as in the first solution. 转换为list方法与第一个解决方案相同。

You can try out this method to get particular number of permutations number of results a permutation produce is n! 你可以尝试这种方法来获得特定数量的排列结果,排列产生的结果是n! where n stands for the number of elements in a list for example if you want to get only 2 results then you can try the following: 其中n代表列表中元素的数量,例如,如果您只想获得2个结果,那么您可以尝试以下方法:

Use any temporary variable and limit it 使用任何临时变量并限制它

    from itertools import permutations
    m=['a','b','c','d']
    per=permutations(m)
    temp=1
    for i in list(per):
        if temp<=2:    #2 is the limit set
           print (i)
           temp=temp+1
        else:
           break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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