我想根据表2中的id合并两个列表,显示在表1中。

table1 = [(u'Id1', u'New'),(u'Id4', u'New')]
table2 = [(u'Id1', u'Proudct1', None, u'New', u'Id#343', u'Jim'),
          (u'Id2', u'Proudct2', None, u'New', u'Id#3343', u'Jim')]

Combined = [item for item in table2 if item[0] in table1]
print Combined

结果:
[]

预期结果:
[(u'Id1', u'Proudct1', None, u'New', u'Id#343', u'Jim')]

最佳答案

你的问题正好在这里:

if item[0] in table1

相反,您应该将元组的第一项item[0]与表1中的第一项进行比较
if item[0] in [elment[0] for element in table1]

09-25 18:03
查看更多