Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of MySQL "Left Join" two lists of tuples

This seems fundamental (though I do come from a MySQL background).

a = [(1,f,e),(7,r,None),(2,s,f),(32,None,q)]
b = [(32,dd), (1,pp)]

If I do this in MySQL (LEFT JOIN):

SELECT a.*, b.* FROM a LEFT JOIN b ON a[0] = b[0]

I get:

[(1,f,e,1,pp),(7,r,None,None,None),(2,s,f,None,None),(32,None,q,32,dd)]

How does one accomplish this pythonically?

(Maybe I'm not searching for the right term... but I don't (think) I want to append or merge or concat...)

like image 580
Trees4theForest Avatar asked Nov 01 '25 20:11

Trees4theForest


2 Answers

You can solve it by making a dictionary out of the second input list and then looking up into it:

>>> a = [(1,'f','e'),(7,'r',None),(2,'s','f'),(32,None,'q')]
>>> b = [(32,'dd'), (1,'pp')]
>>>
>>> b_dict = {item[0]: item for item in b}
>>> [item + b_dict.get(item[0], (None, None)) for item in a]
[
    (32, None, 'q', 32, 'dd'), 
    (1, 'f', 'e', 1, 'pp'), 
    (2, 's', 'f', None, None), 
    (7, 'r', None, None, None)
]

Since we are iterating over a to form a resulting list, and looking up values of the second list, this would act as a "LEFT JOIN" - returning all the items from the left "table" even if they are not present in the right "table".

like image 175
alecxe Avatar answered Nov 03 '25 09:11

alecxe


You could choose pandas as a solution. pandas is a python module related with data process, it has MySQL interface and could mock database operation(like filter, join, groupby) in its DataFrame, please check here for detail.

import pandas as pd

# origin data
#a = [(1,f,e),(7,r,None),(2,s,f),(32,None,q)]
#b = [(32,dd), (1,pp)]

# new data
a = [{'a1':1,'a2':'f', 'a3':'e'}, {'a1':2, 'a2':'r', 'a3':None}]
b = [{'b1':32, 'b2':'dd'}, {'b1':1, 'b2':'pp'}]

pd_a = pd.DataFrame(a)
pd_b = pd.DataFrame(b)

result = pd.merge(pd_a, pd_b, left_on='a1', right_on='b1', how='left')
print result

output as below:

   a1 a2    a3  b1   b2
0   1  f     e   1   pp
1   2  r  None NaN  NaN
like image 30
linpingta Avatar answered Nov 03 '25 11:11

linpingta