How to get all combinations of list elements of size k (if list, k > length) in Python?


Kronal kp:

This doesn't print anything:

from itertools import combinations
comb = combinations([1,2], 3)
for i in comb:
    print(i)

I want the output to be:

(1,2,2) (1,2,1) (1,1,2) (1,1,1) (2,1,2) (2,1,1) (2,2,1) (2,2,2)
ShadowRanger:

It seems that you only want product, instead of combinations:

from itertools import product

for i in product([1, 2], repeat=3):
    print(i)

combinationsGives you unique combinations without reusing elements from any combination, which means it's impossible to pull three elements from two source elements. It's also designed to be insensitive, so it won't give you (1, 2)and (2, 1)even if you're only asking for a combination of size 2(and will only permutationsdo it). In your case, you seem to want to loop through each element of each index, allowing duplicates (which combinations/ permutationswon't do) and order-sensitive ( which/wo combinations_with_replacementn't do), and it leaves product.

Related


get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as itertools.combinations(iterable, r) in python So far I came up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combinatio

Get all possible combinations of k elements from a list

Tobias Herman I need a function that does the same thing as in pythonitertools.combinations(iterable, r) So far I have come up with this: {-| forward application -} x -: f = f x infixl 0 -: {-| combinations 2 "ABCD" = ["AB","AC","AD","BC","BD","CD"] -} combin

An efficient way to get all k combinations of a list in python

loris itertools.combinations(iterable,k)For large test cases, I use a rather time-consuming approach . Is there any way to make it faster. For example, I have a list of numbers:[1,2,3] List of all possible "2" combinations:[(1,2),(1,3),(2,3)] Any help is appre

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

Python: Get all combinations of sequential elements of a list

Minatverma Given an array say x = ['A','I','R']I want the output as [['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']] What I don't want as output is: [['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequen

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

How to get all possible combinations of list elements?

Ben : I have a list of 15 numbers and I need to write some code to generate all 32,768 combinations of these numbers. I've found some code (by googling) that apparently does what I want, but I've found the code to be rather opaque and have been cautious about

Get all n-size combinations with k-size letters

Scion 4581 Can someone help me? I am trying to find the formula and write a piece of code in PHP language which makes the next step Imagine we have 3 types of things, k = 1, 2, 3, and the length of this number can be different (n length), but adjacent types sh

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

Get all combinations of a list in Python

CMinusMinus: I want to get all combinations of lists: L = ["a","b","c"] combinations(L,length=2) # [("a","a"),("a","b"),("a","c"),("b","a"),("b","b"),("b","c"),("c","a"),("c","b"),("c","c")] I tried itertools.combinations() but it's back [('a', 'b'), ('a', 'c

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>

How to get all combinations of a list?

TheRealTengri: I am trying to get all combinations of a list. Here is an example: >>> l = [1, 2, 3] >>> combo = something >>> print(combo) [1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321] Here's what I've tried so far: >>> import itertools >>>