Jonathan Berglund
Jonathan Berglund

Reputation: 23

Concatenate two matrices based on matching list in Python?

I want to concatenate two matrices based on its matching string-values in a specific column. For example, I am trying to combine:

1 2 a
3 4 b
5 6 c
7 8 d

and

13 14 c
15 16 d
9 10 a
11 12 b

Such as:

1 2 9 10 a
3 4 11 12 b
5 6 13 14 c
7 8 15 16 d

Observe that the matrices aren't sorted in the same way order, but that I wish for the result to be sorted similar to the first one.

Thanks!

Upvotes: 2

Views: 258

Answers (3)

vash_the_stampede
vash_the_stampede

Reputation: 4606

l1 = [[1,2,'a'],[3,4,'b'],[5,6,'c'],[7,8,'d']]
l2 = [[13,14,'c'],[15,16,'d'],[9,10,'a'],[11,12,'b']]
l3 = sorted(l1, key=lambda x: x[2])
l4 = sorted(l2, key=lambda x: x[2])

l = list(zip(l3,l4))
z = [list(set(x + y)) for x, y in l]
[[1, 2, 9, 10, 'a'], [3, 4, 'b', 11, 12], [5, 6, 13, 14, 'c'], [7, 8, 15, 16, 'd']]

Upvotes: 1

msi_gerva
msi_gerva

Reputation: 2078

Not as elegant as Pandas (jpp answer), but another way using plain Lists and Dictionaries:

list_a=[[1,2,'a'],[3,4,'b'],[5,6,'c'],[7,8,'d']]
list_b=[[13,14,'c'],[15,16,'d'],[9,10,'a'],[11,12,'b']];
# ---------------------------------------
dict_result = {val[2]:val[0:2] for val in list_a}
for val in list_b:
    dict_result[val[2]].extend(val[0:2])
# -----------------------------------------
result=[];
for key,val in dict_result.iteritems():
    val.extend(key)
    result.append([valout for valout in val]);
# ------------------------------------------
print result

Upvotes: 0

jpp
jpp

Reputation: 164783

You don't have a matrix there, since a matrix or array (with NumPy) typically indicates numeric data only. Also, you are looking to merge data rather than concatenate. If you are happy to use a 3rd party library, this is possible with Pandas:

import pandas as pd

df1 = pd.DataFrame([[1, 2, 'a'], [3, 4, 'b'], [5, 6, 'c'], [7, 8, 'd']])
df2 = pd.DataFrame([[13, 14, 'c'], [15, 16, 'd'], [9, 10, 'a'], [11, 12, 'b']])

res = df1.merge(df2, on=2).values.tolist()

print(res)

[[1, 2, 'a', 9, 10],
 [3, 4, 'b', 11, 12],
 [5, 6, 'c', 13, 14],
 [7, 8, 'd', 15, 16]]

Upvotes: 2

Related Questions