Python matching - python

I have 3 great lists and I would like to make 1: 1: 1 relationships with them. To be clearer I will show the lists:
lstA= [[[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (16, 0)], [(8, 1), (9, 1), (10, 1), (16, 0)], [(11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 0)]], [[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (17, 0)], [(8, 1), (9, 1), (10, 1), (17, 0)], [(11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (17, 0)]], [[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (18, 0)], [(8, 1), (9, 1), (10, 1), (18, 0)], [(11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (18, 0)]], [[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (19, 0)], [(8, 1), (9, 1), (10, 1), (19, 0)], [(11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (19, 0)]]]
lstB= [[[1, 1, 1, 1, 1, 1, 1, 1, -2], [1, 1, 1, -1], [1, 1, 1, 1, 1, -1]], [[1, 1, 1, 1, 1, 1, 1, 1, -2], [1, 1, 1, -1], [1, 1, 1, 1, 1, -1]], [[1, 1, 1, 1, 1, 1, 1, 1, -3], [1, 1, 1, -1], [1, 1, 1, 1, 1, -2]], [[1, 1, 1, 1, 1, 1, 1, 1, -3], [1, 1, 1, -1], [1, 1, 1, 1, 1, -2]]]
lstC= [[[8], [3], [5]], [[8], [3], [5]], [[8], [3], [5]], [[8], [3], [5]]]
Each of these lists consists of 4 lists, which at the same time, are composed of 3 lists. I need to match and print each element of a list with the one corresponding to the same position of the other lists:
As an example, the first matched element will be:
[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (16, 0)]), [1, 1, 1, 1, 1, 1, 1, 1, -2], [8]
The second matched element will be:
[(8, 1), (9, 1), (10, 1), (16, 0)], [1, 1, 1, -1], [3]
Any suggestions to make this?

You could use zip, as suggested by Julien and Juanpa in the comments, but then you'd have to flatten lists:
for i in range(len(lstA)):
for elt in zip(lstA[i], lstB[i], lstC[i]):
print elt
Instead, you could also use nested for loops:
for i in range(len(lstA)):
for j in range(len(lstA[i])):
print lstA[i][j], lstB[i][j], lstC[i][j]
For both solutions, if there are instances where the lists have different numbers of elements, you'll run into trouble.

Related

Rearrange the tuple by sorting in python

I have two lists. One list contains X coordinate values and second list contains Y coordinate values. Using these two lists, I want to make a tupple which is sorted by their first element.
X coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
I want my output like this:
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]
To achieve this result, I wrote below code and got an output mentioend below.
merged_list = list(tuple(zip(X3_coordinate, Y3_coordinate)))
merged_list.sort(key=lambda x: x[0])
merged_list
Output:
[(0, 4), (0, 3), (1, 4), (1, 3), (1, 2), (1, 1), (1, 0), (2, 3), (2, 4), (2, 0), (2, 1), (2, 2), (3, 3), (3, 4), (4, 3), (4, 4)]
Kindly let me know what I am doing wrong and give some suggestions of code.
instead of list(tuple()) just do list()
sort with no key, it'll do element-wise by default
use sorted do both generate the list and sort
X_coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y_coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
merged_list = sorted(zip(X_coordinate, Y_coordinate))
print(merged_list)
You've sorted by x, but not later by y:
merged_list.sort(key=lambda x: (x[0], x[1]))
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]
By using "key=lambda x: x[0]", you are forcing it to be sorted only by the first element, what you seek is a sort where-in you give priority to the first element, but if the values are same you wish to sort it by the subsequent elements.
X3_coordinate = [2, 3, 4, 4, 3, 2, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2]
Y3_coordinate = [3, 3, 3, 4, 4, 4, 4, 4, 3, 3, 2, 1, 0, 0, 1, 2]
merged_list = list(zip(X3_coordinate, Y3_coordinate))
merged_list.sort()
print(merged_list)
Output:
[(0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 3), (4, 4)]

Special kind of permutation

I am working on some code to produce all possible permutations from a list. Such products are constrained exclusively to one specific size. My code works for regular permutations, but I do not know how to obtain permutations that follow this type of logic. I'm supposed to get all of the permutations that follow this sort of criss-cross pattern, where the first element is followed by another element and then repeated until the product reaches size z, into one big array.
For example, given an array like [0,1,2,3] and z=3,
One should return something like [[0,1,0][1,0,1][2,0,2][0,2,0][0,3,0][0,4,0]...]
Of course the output would change based on z, so, if we had z=4 the array would have subarrays that are of size 4, like [[0,1,0,1],[1,0,1,0],[2,0,2,0],[0,2,0,2], etc.]
Fundamentally, you just need to alternate two elements in the original array so they form that type of a-b/a-b-a/a-b-a-b/pattern, so two elements at a time are taken.
This is my code for basic permutations with size z.
def comb(k,array):
if k<=0:
return [[]]
else:
final=[]
for part in comb(k-1,array):
for e in array:
final.append([e]+part)
return final
I don't want to use any library because I am studying recursion specifically and that would be sort of pointless learning-wise. Thank you.
Use itertools.product:
from itertools import product
lst = [0,1,2,3]
result = list(product(lst,repeat=3))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 0, 0), (3, 0, 1), (3, 0, 2), (3, 0, 3), (3, 1, 0), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 0), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
Try permutations from itertools module:
l = [0, 1, 2, 3]
z = 3
out = list(permutations(l, r=z))
print(out)
# Output:
[(0, 1, 2),
(0, 1, 3),
(0, 2, 1),
(0, 2, 3),
(0, 3, 1),
(0, 3, 2),
(1, 0, 2),
(1, 0, 3),
(1, 2, 0),
(1, 2, 3),
(1, 3, 0),
(1, 3, 2),
(2, 0, 1),
(2, 0, 3),
(2, 1, 0),
(2, 1, 3),
(2, 3, 0),
(2, 3, 1),
(3, 0, 1),
(3, 0, 2),
(3, 1, 0),
(3, 1, 2),
(3, 2, 0),
(3, 2, 1)]
Check the code of permutations which is roughly equivalent.
For z=4:
l = [0, 1, 2, 3]
z = 4
out = list(permutations(l, r=z))
print(out)
# Output:
[(0, 1, 2, 3),
(0, 1, 3, 2),
(0, 2, 1, 3),
(0, 2, 3, 1),
(0, 3, 1, 2),
(0, 3, 2, 1),
(1, 0, 2, 3),
(1, 0, 3, 2),
(1, 2, 0, 3),
(1, 2, 3, 0),
(1, 3, 0, 2),
(1, 3, 2, 0),
(2, 0, 1, 3),
(2, 0, 3, 1),
(2, 1, 0, 3),
(2, 1, 3, 0),
(2, 3, 0, 1),
(2, 3, 1, 0),
(3, 0, 1, 2),
(3, 0, 2, 1),
(3, 1, 0, 2),
(3, 1, 2, 0),
(3, 2, 0, 1),
(3, 2, 1, 0)]

Dynamic Nested Loops Python

This is the array I require:
N = 6
A = [[x,y,z] for x in range(N+1) for y in range(N+1) for z in range(N+1) if x+y+z== N]
Is there any other way of doing this by only specifying the variable N and 3 instead of x,y,z?
I tried []*3 but can't get the required output.
You are looking for iterttols.product:
from itertools import product
N = 6
A = [(x,y,z) for x in range(N+1) for y in range(N+1) for z in range(N+1) if x+y+z== N]
B = [tup for tup in product(range(N+1), repeat=3) if sum(tup) == N]
print(A, B, A == B, sep='\n')
Gives:
[(0, 0, 6), (0, 1, 5), (0, 2, 4), (0, 3, 3), (0, 4, 2), (0, 5, 1), (0, 6, 0), (1, 0, 5), (1, 1, 4), (1, 2, 3), (1, 3, 2), (1, 4, 1), (1, 5, 0), (2, 0, 4), (2, 1, 3), (2, 2, 2), (2, 3, 1), (2, 4, 0), (3, 0, 3), (3, 1, 2), (3, 2, 1), (3, 3, 0), (4, 0, 2), (4, 1, 1), (4, 2, 0), (5, 0, 1), (5, 1, 0), (6, 0, 0)]
[(0, 0, 6), (0, 1, 5), (0, 2, 4), (0, 3, 3), (0, 4, 2), (0, 5, 1), (0, 6, 0), (1, 0, 5), (1, 1, 4), (1, 2, 3), (1, 3, 2), (1, 4, 1), (1, 5, 0), (2, 0, 4), (2, 1, 3), (2, 2, 2), (2, 3, 1), (2, 4, 0), (3, 0, 3), (3, 1, 2), (3, 2, 1), (3, 3, 0), (4, 0, 2), (4, 1, 1), (4, 2, 0), (5, 0, 1), (5, 1, 0), (6, 0, 0)]
True
Use itertools.combinations_with_replacement:
from itertools import combinations_with_replacement
n = 6
k = 3
# If you want a list of tuples:
lst = [item for item in list(combinations_with_replacement(range(n), k)) if sum(item) == n]
print(lst)
# [(0, 1, 5), (0, 2, 4), (0, 3, 3), (1, 1, 4), (1, 2, 3), (2, 2, 2)]
# If you want a list of lists:
lst = [list(item) for item in list(combinations_with_replacement(range(n), k)) if sum(item) == n]
print(lst)
# [[0, 1, 5], [0, 2, 4], [0, 3, 3], [1, 1, 4], [1, 2, 3], [2, 2, 2]]

How to get unique combinations of pairs in nested tuples?

I have a list of pairs of the form [(tuple1), (tuple2)],where the first tuple has variable length and the second has length 1.
Example:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1),
((12,), 1),
((3, 4, 5), 0)]
I want set(pairs), where tuple1_pair_X = tuple1_pair_Y && tuple2_pair_X = tuple2_pair_Y and not all the possible combinations of tuple1 elements with tuple2.
Desired Output:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1)]
If you want to keep order of your list:
lst = [((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1),
((12,), 1),
((3, 4, 5), 0)]
sorted(set(lst), key=lst.index)
outputs:
[((0, 1, 2), 0),
((3, 4, 5), 0),
((12,), 1),
((0, 1, 4, 7), 1)]
I guess simply using the set() command on the original list of tuples should work and give your desired output.
tup_list = [((0, 1, 2), 0), ((3, 4, 5), 0),((12,), 1),((0, 1, 4, 7), 1),((12,), 1),((3, 4, 5), 0)]
output = list(set(tup_list))

Merge list of list with duplicates

Input list
[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273']
[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060']
[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
And the result should be like:
[(1, 0, 3), ['21:40:44', '21:43:37', '21:45:27'], (1, 3, 9), (1, 4, 6), ['.273', '.060', '.410']
The goal is to reduce the duplicates, the unique elements in a column should be stored in a list.
How about using zip. It will merge the 3 list and give you output with duplicates. later use set to remove duplicates. and since you don not want list of tuple for single element only, use list(set(x))[0] if len(set(x))==1 else list(set(x))
a=[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273']
b=[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060']
c=[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
l=[list(set(x))[0] if len(set(x))==1 else list(set(x)) for x in list(zip(a,b,c))]
print(l)
>>>[(1, 0, 3), ['21:45:27', '21:40:44', '21:43:37'], (1, 3, 9), (1, 4, 6), ['.060', '.273', '.410']]
the simplest way is this:
in_data = [
[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273'],
[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060'],
[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
]
res = []
for i in range(len(in_data[0])):
data = list(set([x[i] for x in in_data ]))
data = data[0] if len(data) == 1 else data
res.append(data)
print(res)
>>> [(1, 0, 3), ['21:43:37', '21:40:44', '21:45:27'], (1, 3, 9), (1, 4, 6), ['.273', '.060', '.410']]

Categories