Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Subsets() with same element multiple times?

Essentially, I want to use:

ListA = [1,2,3]
S = some_function(ListA, 2)
list(S)

And receive in return

[[1,1], [1,2], [1,3], [2,2], [2,3], [3,3]]

Theoretically, some_function() ought to be Subsets() with some options, but that doesn't seem available. Is there a good way to do this other than a multiple for loop? You can work around how Subsets() works with:

ListB = []
for i in ListA:
    for j in ListA:
        A = sorted([i,j])
        if A not in ListB:
            ListB.append(A)
sorted(ListB)

But that gets tedious (and probably time-consuming) when you want 6-element subsets--and extra annoying if you want to be able to change the length with a variable. It seems like this ought to be an included functionality; what some_function() am I missing?

Subsets() with same element multiple times?

Essentially, I want to use:

ListA = [1,2,3]
S = some_function(ListA, 2)
list(S)

And receive in return

[[1,1], [1,2], [1,3], [2,2], [2,3], [3,3]]

Theoretically, some_function() ought to be Subsets() with some options, but that doesn't seem available. Is there a good way to do this other than a multiple for loop? You can work around how Subsets() works with:

ListB = []
for i in ListA:
    for j in ListA:
        A = sorted([i,j])
        if A not in ListB:
            ListB.append(A)
sorted(ListB)

But that gets tedious (and probably time-consuming) when you want 6-element subsets--and extra annoying if you want to be able to change the length with a variable. It seems like this ought to be an included functionality; what some_function() am I missing?