2017-09-22 239 views
1
some_list = [[app_num, product, prod_size, prod_amt]] 
other_list = [[app_num, product, prod_size, prod_amt]] 

我有兩個列表。 app_num的some_list和other_list之間沒有匹配。但是,對於other_list中的給定app_num,產品prod_size_和prod_amt可能與some_list中給定app_num中的相同。我正在嘗試創建一個與some_list相同的final_some_list,除了它已從some_list中刪除任何列表元素(如果該列表元素具有相同的product,prod_size和prod_amt作爲other_list中的元素)。Python - 比較兩個嵌套列表並寫入第三個嵌套列表

我已經嘗試嵌套for循環,列表理解(下面是許多失敗嘗試的一些例子)。

final_some_list = [app for app in some_list for other_app in other_list 
if not app[1] == other_app[1] and app[2] == other_app[2] and app[3] == 
other_app[3] 

final_some_list = [app for app in some_list for other_app in other_list 
if not app[1] and app[2] and app[3] == in other_app 
+0

你能分享輸入的實例和期望的輸出,使它讓任何人都更容易理解你的問題? –

回答

2
final_some_list = [x for x in some_list if all(x[1:] != y[1:] for y in other_list)] 
+0

非常優雅,謝謝! – vintagedeek

0

只有第一個比較包含not屬性,添加=所有的比較,而這應該工作:

final_some_list = [app for app in some_list for other_app in other_list 
if app[1] != other_app[1] and app[2] != other_app[2] and app[3] != 
other_app[3]]