Finding unique triplets from list in Python - python

I am writing some research script in python and I have a list of 9 elements:
a = [1,2,3,4,5,6,7,8,9]
I would like to find all combinations of subsets containing the 3 elements (and NOT the 3 element sets themselves), so for example:
a_new = [ [ [1,2,3],[4,5,6],[7,8,9] ] ],
[ [2,3,4],[5,6,7],[8,9,1] ],
[ [3,4,6],[1,2,8],[5,7,9] ], etc.... ]
the elements are not repeated in the subsets
the sets [3,2,1] and [1,2,3] are the same set and are not unique
Have you perhaps faced such a case, or some Python library will handle it?
Greetings

Use itertools.combinations as follows:
from itertools import combinations
a = {1, 2, 3, 4, 5, 6, 7, 8, 9}
# pick 3
for c_out in combinations(a, r=3):
s_out = set(c_out)
# pick the next 3 from the those elements that are not already picked (i.e not in s_out)
for c_in in combinations(a - s_out, r=3):
# there only 3 elements left so only one way to pick
rest = tuple(a - (s_out | set(c_in)))
print(c_out, c_in, rest)
Output (partial)
(1, 2, 3) (4, 5, 6) (8, 9, 7)
(1, 2, 3) (4, 5, 7) (8, 9, 6)
(1, 2, 3) (4, 5, 8) (9, 6, 7)
(1, 2, 3) (4, 5, 9) (8, 6, 7)
(1, 2, 3) (4, 6, 7) (8, 9, 5)
(1, 2, 3) (4, 6, 8) (9, 5, 7)
(1, 2, 3) (4, 6, 9) (8, 5, 7)
(1, 2, 3) (4, 7, 8) (9, 5, 6)
(1, 2, 3) (4, 7, 9) (8, 5, 6)
(1, 2, 3) (4, 8, 9) (5, 6, 7)
(1, 2, 3) (5, 6, 7) (8, 9, 4)
(1, 2, 3) (5, 6, 8) (9, 4, 7)
(1, 2, 3) (5, 6, 9) (8, 4, 7)
(1, 2, 3) (5, 7, 8) (9, 4, 6)
You can read more about set operations (union, difference) in the documentation.

you can use itertools built-in module
from itertools import combinations
a = [1,2,3,4,5,6,7,8,9]
combination = list(combinations(a, 3))
this output in tuple and you can transform to list easily.

Related

All possible combinations of columns and rows in pandas DataFrame

I have this Dataframe that I want to get all possible combinations of this dataframe across both rows and columns.
A_Points
B_Points
C_Points
0
1
1
3
5
4
9
2
4
For example a combination as follows Points = 0 + 5 + 4, or 9 + 1 + 1.
Is there a builtin tool for such problem?
This is what I tried, but it did not give the desired output.
> import itertools
> combined_dataframe = pd.DataFrame({'{}{}'.format(a, b): possible_feature_characteristicpoints[a] - possible_feature_characteristicpoints[b] for a, b in itertools.combinations(possible_feature_characteristicpoints.columns, 2)})
Use itertools.product and sum:
from itertools import product
out = list(map(sum, product(*df.to_numpy().tolist())))
Output:
[12, 5, 7, 14, 7, 9, 13, 6, 8, 13, 6, 8, 15, 8, 10, 14, 7, 9, 13, 6, 8, 15, 8, 10, 14, 7, 9]
Intermediate:
list(product(*df.to_numpy().tolist()))
Output:
[(0, 3, 9),
(0, 3, 2),
(0, 3, 4),
(0, 5, 9),
(0, 5, 2),
(0, 5, 4),
...
(1, 4, 2),
(1, 4, 4)]
Another way with list comprehension,
import pandas as pd
data = {
"A_Points": [0,3,9],
"B_Points": [1,5,2],
"C_Points": [1,4,4],
}
df = pd.DataFrame(data)
result = [ (i, j, k)
for i in df.A_Points
for j in df.B_Points
for k in df.C_Points
]
print(result)
Output:
[(0, 1, 1), (0, 1, 4), (0, 1, 4), (0, 5, 1), (0, 5, 4), (0, 5, 4), (0, 2, 1), (0, 2, 4), (0, 2, 4), (3, 1, 1), (3, 1, 4), (3, 1, 4), (3, 5, 1), (3, 5, 4), (3, 5, 4), (3, 2, 1), (3, 2, 4), (3, 2, 4), (9, 1, 1), (9, 1, 4), (9, 1, 4), (9, 5, 1), (9, 5, 4), (9, 5, 4), (9, 2, 1), (9, 2, 4), (9, 2, 4)]

How to convert list comprehension to for loop in python [duplicate]

This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 1 year ago.
So I have this list comprehension line I need to convert to a for loop in python.
pred and exp are lists that have many different values in them. They are part of a data set of numbers and with machine learning the code is supposed to guess the number(but that is just background)
correct = [(p, e) for (p, e) in zip(pred, exp) if p != e]
Output: of correct
[(8, 2),
(7, 2),
(9, 8),
(4, 0),
(2, 8),
(9, 7),
(8, 9),
(8, 9),
(2, 8),
(1, 8),
(4, 9),
(9, 4),
(3, 8),
(1, 8),
(3, 9),
(9, 5),
(9, 5),
(9, 7),
(3, 8),
(1, 8),
(9, 7),
(9, 7),
(2, 8),
(9, 7),
(3, 9),
(8, 2),
(1, 8),
(7, 3),
(1, 8),
(3, 8),
(9, 7),
(5, 9),
(3, 8),
(2, 8),
(8, 1),
(9, 1),
(1, 2),
(9, 5),
(8, 2),
(4, 5),
(9, 5),
(4, 6),
(2, 3),
(1, 8),
(8, 2),
(9, 3),
(8, 2),
(5, 3),
(9, 5),
(1, 8),
(1, 8),
(3, 9),
(8, 2),
(1, 9),
(1, 9),
(8, 1),
(3, 8),
(2, 7)]
I tried this
correct = []
for (p, e) in zip(predicted, expected):
if p != e:
correct.append(p)
correct.append(e)
print(correct)
But the output looks like this
[5, 3, 6, 8, 1, 4, 7, 9, 7, 3, 7, 8, 7, 9, 7, 4, 7, 9, 9, 8, 9, 8, 6, 8, 8, 2, 8, 2, 3, 8, 1, 8, 1, 2, 1, 8, 1, 8, 9, 5, 7, 9, 7, 9, 1, 8, 9, 5, 1, 8, 8, 2, 7, 2, 8, 9, 3, 8, 9, 5, 8, 9, 6, 3, 7, 9, 8, 6, 0, 2, 2, 8, 9, 5, 9, 5, 7, 4, 9, 5, 1, 8, 1, 8, 5, 9, 1, 2, 7, 4, 8, 2]
Any help would be appreciated, thanks
You want a list of tuples, but you get a list of numbers. The reason is that you are appending the numbers just as numbers, not as tuples.
Instead, do
if p != e:
correct.append((p,e))
Inside the for loop you're appending p and e to the list as single elements, despite the fact that on each iteration you get them inside a tuple.
To fix it just append them to the list as a tuple element.
if p != e:
correct.append((p,e))

Given a list of lists, how do I create a matrix of all possible combinations of values in Python? [duplicate]

This question already has an answer here:
Outputting all possible permutations of multiple lists
(1 answer)
Closed 1 year ago.
I know how to create a matrix of combinations, but I want something more dynamic. For example the below creates a matrix with all possible combinations of A, B and C:
A = [0, 1, 2]
B = [3, 5, 7]
C = [10, 20, 30]
MATRIX = []
for a in A:
for b in B:
for c in C:
MATRIX.append([a, b, c])
I would like something that is more dynamic where I could have A, B, C, .....N lists. All I have to define is the values within each list and then I want it to produce a Matrix of all possible combinations.
How do I do this?
Using itertools.product you can pass any number of iterables:
from itertools import product
print(list(product(A, B, C)))
output :
[(0, 3, 10), (0, 3, 20), (0, 3, 30), (0, 5, 10), (0, 5, 20), (0, 5, 30), (0, 7, 10), (0, 7, 20), (0, 7, 30), (1, 3, 10), (1, 3, 20), (1, 3, 30), (1, 5, 10), (1, 5, 20), (1, 5, 30), (1, 7, 10), (1, 7, 20), (1, 7, 30), (2, 3, 10), (2, 3, 20), (2, 3, 30), (2, 5, 10), (2, 5, 20), (2, 5, 30), (2, 7, 10), (2, 7, 20), (2, 7, 30)]
If you want to have a list of lists, not list of tuples, just write a simple "list comprehension".

Array concatenation in Python

I am new to Python and I would like to combine two arrays. I have two arrays: A and B and would like to get array C as follows:
A = [1, 2, 3, 4, 5]
B = [6, 7, 8, 9, 10]
Result array:
C = [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
Use zip, the built-in for pairwise iteration:
C = list(zip(A, B))
Note that you are not concatenating the two lists.
Without any dependency:
>>> A = [1, 2, 3, 4, 5]
>>> B = [6, 7, 8, 9, 10]
>>> C = [(A[i], B[i]) for i in range(len(A))]
>>> C
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]

Converting list of points to list of tuples in python

I've just written some code to convert a list of points into a list of to/from tuples but it doesn't feel very efficient.
I was wondering if anyone had any suggestions to make this more concise?
from_point, to_point = None, None
point_list = []
for p in [1, 5, 2, 4, 7, 9]:
to_point = p
if from_point and to_point:
point_list.append((from_point, to_point))
from_point = to_point
print(point_list)
Input: [1, 5, 2, 4, 7, 9]
Output: [(1, 5), (5, 2), (2, 4), (4, 7), (7, 9)]
Edit: Changed points to be non sequential.
You can always use zip:
>>> p = [1, 5, 2, 4, 7, 9]
>>> point_list = list(zip(p[:-1], p[1:]))
>>> print(point_list)
[(1, 5), (5, 2), (2, 4), (4, 7), (7, 9)]
An alternative one line solution
input = [1, 2, 3, 4, 5, 6]
output = [(input[index], input[index+1]) for index in range(len(list)-1)]
print(output)
What about this?
x=[1, 5, 2, 4, 7, 9]
print [ tuple(x[i:i+2]) for i in xrange(len(x)-1) ]
Using more_itertools:
import more_itertools as mit
list(mit.pairwise([1, 5, 2, 4, 7, 9]))
# [(1, 5), (5, 2), (2, 4), (4, 7), (7, 9)]

Categories