This question already has an answer here:
Having problems with python permutations
(1 answer)
Closed 2 years ago.
How to generate all possible ordering of a set of elements in a list which:
generates all ordering (not only those with sorted order)
allows repetition
I know that using itertools.permutations(iterable[, r]) I can do the following:
import itertools
a = itertools.permutations([1,2])
list(a)
output:
[(1, 2), (2, 1)]
and also using the itertools.combinations_with_replacement(iterable, r):
import itertools
a = itertools.combinations_with_replacement([1,2],2)
list(a)
output:
[(1, 1), (1, 2), (2, 2)]
but what I want is a function which does the following:
a = function([1,2],2)
output:
[(1, 1), (1, 2), (2, 1), (2, 2)]
In other words, for a list of size n with distinct elements and a given input of r it could generate all possible n**r possible combinations
itertools.product([1,2],repeat=2)
I think...
Related
This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 4 months ago.
Take the list [1,2,3], I would like to be able to find all ordered combinations of this list without repetition that are the same length as this list. In my case, I need to be able to generate (1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1). How can I go about doing this? I cannot seem to find a function within itertools to allow for this to happen.
I have tried to use other parts of itertools to generate all the possible lists of n items, but found this scales poorly for large n.
itertools.permutations is your friend:
>>> from itertools import permutations
>>> list(permutations([1,2,3]))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
This question already has an answer here:
How to get combinations of elements from a list?
(1 answer)
Closed 4 years ago.
Let's say I have a list of four values. I want to find all combinations of two of the values. For example, I would like to get an output like:
((0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3))
As you can see, I do not want repetitions, for example (0, 1) and (1, 0)
This needs to be able to be used with larger numbers, not just 4, and I will have to iterate through all of the combos
I am using Python 3 and Windows, and this would ideally be an inbuilt function, a simple bit of list comprehension code, or something I can import. I have tried making this with range, but I do not know how to exclude the numbers that I have already done from it.
It is very easy
from itertools import combinations
list(combinations([0,1,2,3],2))
Take just the lower triangular matrix if you only need a distinct set
a = [1,2,10,20]
[(a[i], a[j+i+1]) for i in range(len(a)) for j in range(len(a[i+1:]))]
[(1, 2), (1, 10), (1, 20), (2, 10), (2, 20), (10, 20)]
This question already has answers here:
Get all pairwise combinations from a list
(2 answers)
Closed 2 years ago.
I would like to get all the possible couples of elements from a list like the following :
L = [1, 2, 4, 5]
--> couples = [(1, 2), (1, 4), (1, 5), (2, 4), (2, 5), (4, 5)]
I do not want symetric couples like (1, 2), (2, 1) and I do not want couples with same numbers either like (1, 1)
If you guys could tell me the best way to do it I would be super glad !
A simple way of getting combinations
def getPairs(inList):
outList = []
for i in range(len(inList)):
item = inList[0]
inList = inList[1:]
for j in inList:
outList.append((item, j))
return outList
This question already has answers here:
How To Merge an Arbitrary Number of Tuples in Python?
(7 answers)
Closed 2 years ago.
[(1, 2), (3, 4)]
how can I add element of 2nd tuple of list into 1st tuple ?
I want the final output as : [(1,2,3,4)]
Just try itertools
import itertools
out = [tuple(itertools.chain.from_iterable(ls))]
or else, If you want the lambda function please try below:
flatten = lambda l: [tuple([item for sublist in l for item in sublist])]
flatten(ls)
Basically, you cannot edit or add the elements, but you can create a tuple by iterating the values inside tuples.
In the simple case, that's the simple solution:
l = [(1, 2), (3, 4)]
res = [l[0] + l[1]]
result:
[(1, 2, 3, 4)]
first iteration for and inner for for elements.
x = [(1, 2), (3, 4)]
y=[]
for i in x:
for e in i:
y.append(e)
print(y)
This question already has an answer here:
How to get combinations of elements from a list?
(1 answer)
Closed 4 years ago.
Let's say I have a list of four values. I want to find all combinations of two of the values. For example, I would like to get an output like:
((0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3))
As you can see, I do not want repetitions, for example (0, 1) and (1, 0)
This needs to be able to be used with larger numbers, not just 4, and I will have to iterate through all of the combos
I am using Python 3 and Windows, and this would ideally be an inbuilt function, a simple bit of list comprehension code, or something I can import. I have tried making this with range, but I do not know how to exclude the numbers that I have already done from it.
It is very easy
from itertools import combinations
list(combinations([0,1,2,3],2))
Take just the lower triangular matrix if you only need a distinct set
a = [1,2,10,20]
[(a[i], a[j+i+1]) for i in range(len(a)) for j in range(len(a[i+1:]))]
[(1, 2), (1, 10), (1, 20), (2, 10), (2, 20), (10, 20)]