You need to use itertools.islice
, like this
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))
You could create a tiny wrapper around 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. Convert it to list
by using the list
constructor
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
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
For what you are trying to do, what qualifies as a combination? Does a combination consist of 33 ones, 33 twos and 66 zeros, but in any order?,Letters are like there are 33 ones, 33 twos and 66 zeros amounting to total letters 132. If I do it manually, there will be a lot of combinations. To make it easy for computer I want to limit combinations no to 1000. Plz help. my_letters=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],… there will be a lot of combinations.,Technically, the different orders of the same numbers of each kind of object are known as permutations. Is this what is needed here?
Sorry Ali123, I have no idea what you are talking about here. Your
variable letters doesn’t have any letters in it, it is a list with a
single large number:
11111111111111111111111111
Let’s create 1000 combinations of letters, but to save space, we will display only 10 of those combinations.
import random
combos = []
for k in range(1000):
letters = [random.randint(0, 1) for i in range(26)]
combo = "".join([chr(i + ord("A")) for i in range(26) if letters[i] == 1])
combos.append(combo)
for i in range(10):
print(combos[i])
Output:
BEFHJNXZ ABCFGHIORUVW BDEFKNQRSTYZ ABCHKLMORSTUVWXY ADFHKMNOPQRSTUZ EFKLMNOSUVWZ CDEGHIJLMQVW CDGHIKLMNOPRSTUVXZ ABIJKOPQRSVXYZ BCDEGIKMPTVWXY
In the above techniques to calculate permutations, we are including all the numbers or letters. There is a possibility to restrict the number of elements in the permutations.,Permutation mathematically refers to “the arrangement of certain numbers or letters”. The permutations() function in the itertools library does exactly that.,The permuatations() function takes an iterable argument, therefore in order to find out permutations of numbers, we need to pass the numbers as a list, set, or tuple.,In the above code snippet, the permutations() function is asked to arrange only 2 elements at a time from the list of numbers provided.
import itertools
import itertools
st = "ABC"
per = itertools.permutations(st)
for val in per:
print( * val)
A B C
A C B
B A C
B C A
C A B
C B A
<itertools.permutations object at 0x7fc9abdd8308>
import itertools
values = [1, 2, 3]
per = itertools.permutations(values)
for val in per:
print( * val)
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Last Updated : 19 Jul, 2022
Output:
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
Output:
(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)
Output:
(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)
Permutation is the method of selecting elements from a set in different ways.,For example: the number of ways in which characters from yup can be selected are yup, ypu, uyp, upy, puy, pyu, and not selecting any.,The elements of words are swapped. In this way, we achieve all different combinations of characters.,In this example, recursion is used to find the permutations of a string yup.
Example 1: Using recursion
def get_permutation(string, i = 0): if i == len(string): print("".join(string)) for j in range(i, len(string)): words = [c for c in string ] # swap words[i], words[j] = words[j], words[i] get_permutation(words, i + 1) print(get_permutation('yup'))
Output
yup
ypu
uyp
upy
puy
pyu
None
Example 2: Using itertools
from itertools
import permutations
words = [''.join(p) for p in permutations('pro')]
print(words)