How to use Python left outer join using FOR/LIST/DICTIONARY comprehensions (not SQL)?

≯℡__Kan透↙ 提交于 2019-12-04 23:54:33

问题


I have two tuples, details below:

t1 = [
['aa'],
['ff'],
['er']
]

t2 = [
['aa', 11,],
['er', 99,]
]

and I would like to get results like these below using python method similar to SQL's LEFT OUTER JOIN:

res = [
['aa', 11,],
['ff',  0,],
['er', 99,]
]

Please help me with this.


回答1:


d2 = dict(t2)
res = [[k[0], d2.get(k[0], 0)] for k in t1]


来源:https://stackoverflow.com/questions/15099022/how-to-use-python-left-outer-join-using-for-list-dictionary-comprehensions-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!