Zamarion
Zamarion

Reputation: 103

Generate all vector that respect a condition in Python

Is there a way to generate a list of all the vector of length n witch the element are between 1 and 6 like this : this ? I only know how to do it for a specific n with nested for loop. Thank you

Upvotes: 0

Views: 63

Answers (1)

Jared Goguen
Jared Goguen

Reputation: 9010

A function which takes a pool and a length:

from itertools import product

def generate_vectors(pool, length):
    return list(product(pool, repeat=length))

generate_vectors(range(1,7), 3)
# [(1, 1, 1), (1, 1, 2), (1, 1, 3), ...]

Upvotes: 3

Related Questions