[Python] Fastest way to generate n combinations in python

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have an array of 10 elements and am looking for the fastest way to randomly generate n combinations of size m in Python with those 10 elements.

    I have to do that for all possible sizes of the combinations (m goes from 1 to the number of elements) and n also varies, it increases when the number of combination possible increases.

    For example if my input array is: [a, b, c, d, e, f, g, h, i, j]
    for n = 4 and m = 3 the output should be:

    (e, b, c)
    (i, a, d)
    (g, j, e)
    (i, f, e)

    There can't be twice the same element in one permutation.


    I am aware of the itertool function that generates all combination of a given size, but I only need n combinations, not all. So I am not sure that using itertools is the fastest solution for my problem (since I have then to select randomly n combinations among all those generated).

    Continue reading...

Compartilhe esta Página