Python - Merging 2 lists of tuples by checking their values -


i have lists this:

a = [('jon', 12668, 0.0036), ('jessica', 1268, 0.0536), ('jon', 1668, 0.00305), ('king', 16810, 0.005)] b = [('jon', 12668, 0.0036), ('jon', 16680, 0.00305), ('messi', 115, 0.369)] 

i want resultant list like:

result = [(('jon', 12668, 0.0036), ('jon', 12668, 0.0036)), (('jon', 1668, 0.00305), ('jon', 16680, 0.00305)), (('king', 16810, 0.005), none), (none, ('messi', 115, 0.369))] 

i have tried nested loops, sets, map, zip failed achieve output. kindly me out.

convert a , b dictionaries first using first(use str.lower() in it) , third item key , later on loop on union of keys in list comprehension desired output:

>>> pprint import pprint >>> dct_a = {(x[0].lower(), x[2]): x x in a} >>> dct_b = {(x[0].lower(), x[2]): x x in b} >>> out = [(dct_a.get(k), dct_b.get(k)) k in set(dct_a).union(dct_b)] >>> pprint(out) [(('jon', 12668, 0.0036), ('jon', 12668, 0.0036)),  (('jon', 1668, 0.00305), ('jon', 16680, 0.00305)),  (('king', 16810, 0.005), none),  (('jessica', 1268, 0.0536), none),  (none, ('messi', 115, 0.369))] 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -