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
Related
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:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 1 year ago.
I have a list like this, with an even number of elements:
straight_list = [1, 2, 3, 4, 5, 6]
There are six items in this list. I'd like to group them into three pairs, like this:
paired_list = [(1, 2), (3, 4), (5, 6)]
What's the best way to do this? More generally, given a list with 2*n items, what's the best way to create a list of n pairs? I have come up with the following:
straight_list = [1, 2, 3, 4, 5, 6]
paired_list = []
for i in range(len(straight_list)//2):
j = i*2
pair = (straight_list[j], straight_list[j+1])
paired_list.append(pair)
print(paired_list)
# [(1, 2), (3, 4), (5, 6)]
I also can make it work like this, though it's a tongue-twister:
[tuple(x) for x in list(np.array(straight_list).reshape(len(straight_list)//2,2))]
# [(1, 2), (3, 4), (5, 6)]
Is there a more elegant, functional, and/or "Pythonic" way to create this list of pairs?
(In some ways, this is almost an inverse of this question: "How to make a flat list out of a list of lists?")
How about a simple list comprehension?
paired_list = [(straight_list[i], straight_list[i+1]) for i in range(0, len(straight_list), 2)]
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:
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...
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 7 years ago.
I have three arrays/lists:
list1 = range(1,10)
list2 = range(1,10)
list3 = range(1,10)
I want to create an list of tuples containing an assortment of each of the values in my three lists. Essentially I want an output such as:
combo = [(1,1,1), (1,1,2), (1,1,3) ... (1,4,2), (1,4,3) ... (4,5,4), (4,5,5) ... (10,10,9), (10,10,10)]
It seems like such as simple problem, but I don't know where to start.
You can use itertools.product to generate the Cartesian product of lists:
>>> import itertools
>>> list(itertools.product(list1, list2, list3))
[(1, 1, 1),
(1, 1, 2),
(1, 1, 3),
(1, 1, 4),
(1, 1, 5),
(1, 1, 6),
(1, 1, 7),
(1, 1, 8),
(1, 1, 9),
(1, 2, 1),
(1, 2, 2),
(1, 2, 3)
...
itertools.product(list1, list2, list3) returns a generator object - only when you iterate over it will the tuples be returned. In the example code above, list is used to read the contents of the generator into a Python list.
Note that you can also use the repeat parameter of itertools which is much neater when your lists range over the same elements:
list(itertools.product(list1, repeat=3))
If I got it right, you want a Cartesian product of 3 lists? Well, there is a function for that in itertools
import itertools
result = list(itertools.product(list1, list2, list3))
You can also just create a list and append to it using nested loops. Just remember that if you also want the number 10, you need to change your range to range(1, 11) so that it stops at the number 11. It's like a < 11 (Doesn't include the number 11).
combo = []
for i in range(1, 11):
for j in range(1, 11):
for k in range(1, 11):
a = (i, j, k)
combo.append(a)
Use this :
combo=[]
for i in range(1,11):
for j in range(1,11):
for k in range(1,11):
combo.append( (i,j,k) )