【发布时间】:2020-11-13 07:01:45
【问题描述】:
我有 2 个列表,我想根据与 list2 中的元素匹配来过滤 list1:
list1 = [[1,2,3], [4,5,16]]
list2 = [[9,8,50], [7,10,3]]
每个列表中的最后一个元素是唯一的 id(例如 list[0][-1] 是唯一的) 我想从 list1 (list) 中获取在 list2 中的元素中具有相同最后一个元素的元素
在我的例子中
list1[0][-1] is similar to list2[1][-1]
我试试这个:
[x for x, y in zip(list1, list2) if x[-1] == y[-1]]
但它按位置过滤(检查两个列表中具有相同位置的元素) 无论他们的位置如何,我都想检查他们
编辑 预期结果:
[1,2,3] from the list 1 (because hit last element (list[-1]) is the same as last element in a list inside list2)
【问题讨论】:
-
为什么不向我们展示预期的结果?
-
完成,我添加了它
标签: python