简体   繁体   English

如何比较两个二维列表中的元素?

[英]How to compare elements in two 2D lists?

So for example I have two lists like this and I want to compare the lists inside the two list例如,我有两个这样的列表,我想比较两个列表中的列表

a = [[1,2],[3,4],[4,5]]

b = [[1,2],[4,5],[5,6]]

if [1,2] = [1,2] then it should append to another list(also for [4,5]) The point is I can't think of how to get the [4,5] into the new list如果 [1,2] = [1,2] 那么它应该 append 到另一个列表(也适用于 [4,5]) 关键是我想不出如何将 [4,5] 放入新列表

and [3,4] and [5,6] should not be appended和 [3,4] 和 [5,6] 不应附加

Here is the actual problem I am solving这是我正在解决的实际问题

框架

So each list has 4 elements and it will have another column for classification I have 2 datasets and I want to compare the coordinates in order to map the Name of the first table to the Name of the second table所以每个列表有 4 个元素,它将有另一列用于分类我有 2 个数据集,我想比较坐标以便 map 第一个表的名称到第二个表的名称

You can do so with list comprehension您可以通过列表理解来做到这一点

a = [[1,2],[3,4],[4,5]] 
b = [[1,2],[4,5],[5,6]]

print([e for e in a if e in b])

>>> [[1, 2], [4, 5]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM