I would like to be able to do the following:

```
Combinaciones = itertols.product('abcd',repeat=5)
```

Here is where I would like you to select at random and go eliminating each time you choose not to step on it again:

```
for i in combinaciones
```

2

I would like to be able to do the following:

```
Combinaciones = itertols.product('abcd',repeat=5)
```

Here is where I would like you to select at random and go eliminating each time you choose not to step on it again:

```
for i in combinaciones
```

asked by Nicolas 16.05.2017 в 16:46

source
1

If you want to randomly obtain the output of `itertools.product`

the simplest is to use `random.shuffle()`

to reorganize it randomly:

```
import itertools
import random
Combinaciones = list(itertools.product('abcd',repeat=5))
random.shuffle(Combinaciones)
for i in Combinaciones:
print(i)
```

When traversing it with a `for`

you will never repeat elements since in the list there are no repeated elements, you could also create a generator or, if you really want to delete the elements from the list every time you get one, use `list.pop()`

to extract them. It depends on what and how you want to use them.

answered by
16.05.2017 в 17:04

0

Itertools returns iterators, that is, objects that return the elements one by one. I will create a function that remains the one chosen among the current ones:

```
def choice_iterator(it):
choice = next(it)
n=1
for i in it:
n+=1
if random.random() > 1 / n:
choice=i
return choice
```

It is easy to see that the probability that the probability of being selected for each of the numbers of the given iterator is the same:

```
def randomCombinacion(*args,**kargs):
return choice_iterator(itertools.product(*args,**kargs))
```

answered by
16.10.2018 в 12:58