Skip to content Skip to sidebar Skip to footer

Itertools Product To Generate All Possible Strings Of Size 3

Input: pos_1= 'AVNMHDRW' pos_2= 'KNTHDYBW' pos_3= 'KVNGSDRB' Trying to find all possible triplets using one item from pos_1, one from pos_2, and one from pos_3 I'm trying to figu

Solution 1:

Simple:

itertools.product(pos_1, pos_2, pos_3)

This can be iterated over; if you want a list, just pass it to list.

What exactly is the issue?

Edit: This produces tuples of the items from each source. If you want to join them back into strings, you can do that manually when you iterate:

for a, b, c in itertools.product(pos_1, pos_2, pos_3):
    do_something_with(a + b + c)

or to create the list, you can use a list comprehension:

[a + b +cfor a, b,cin itertools.product(pos_1, pos_2, pos_3)]

Solution 2:

See this answer: In python is ther a concise way to a list comprehension with multiple iterators.

In your case:

triples = [ a+b+c forain pos_1 forbin pos_2 forcin pos_3 ]

Solution 3:

You can make such generator using generator expression:

g = (v1+v2+v3  forv1in pos_1 forv2in pos_2 forv3in pos_3)

Post a Comment for "Itertools Product To Generate All Possible Strings Of Size 3"