[英]Remove List from List partial match Python
I am trying to remove a list from a list if it has a partial match如果列表有部分匹配,我正在尝试从列表中删除列表
List 1清单 1
[['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2'],['Person3', 'www.google.co.uk', '2']]
List 2清单 2
[['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2'],['Person1', 'www.google.co.uk', '2']]
Desired Output:所需的 Output:
['Person2', 'www.amazon.co.uk', '2']
At the moment I have the following code with will remove it as long as it is a full match:目前我有以下代码,只要它是完全匹配的,就会将其删除:
[i for i in List1 if i in List2]
but as the number can vary i want to be a able to match on the first two entries so it will not matter what number is in the last column但由于数字可能会有所不同,我希望能够匹配前两个条目,所以最后一列中的数字无关紧要
You can do:你可以做:
l1 = [['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2'],['Person3', 'www.google.co.uk', '2']]
l2 = [['Person1', 'www.google.co.uk', '2'], ['Person2', 'www.amazon.co.uk', '2'],['Person1', 'www.google.co.uk', '2']]
print ([ x for x in l1 if x[0:2] in [ l[0:2] for l in l2 ] ])
Output: Output:
[['Person1', 'www.google.co.uk', '1'], ['Person2', 'www.amazon.co.uk', '2']]
Here you check for each sublist in l1
if values at index 0 and 1 are found in [ l[0:2] for l in l2 ]
.如果在[ l[0:2] for l in l2 ]
中找到索引 0 和 1 的值,则在这里检查l1
中的每个子列表。 The later is the same than l2
at the notable exception that it contains only two index by sublist instead of more.后者与l2
相同,但值得注意的例外是它仅包含两个子列表索引,而不是更多。 So you can use in
.所以你可以in
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.