I have two list of tuples like shown in following order:
我有两个元组列表,如下所示:
a = [(18, 299, 70, 33), (18, 323, 70, 34),
(18, 448, 70, 35), (18, 548, 70, 36), (18, 613, 70, 37)]
b = [(18, 223, 70, 37), (18, 299, 70, 38),
(18, 323, 70, 39), (18, 448, 70, 40), (18, 548, 70, 41), (18, 613, 70, 42)]
What I want:
我想要的是:
1). I am trying to print elements which are in b
but not in a
1)。我正在尝试打印b中但不在a中的元素
2). I want to compare the above shown lists in such a manner that only first three elements of individual tuples are compared irrespective of non-matching last element of that tuple i.e. 37
, 38
, 39
.... So in above case I want my answer to look like:
2)。我想比较上面列出的列表,只比较单个元组的前三个元素,而不管该元组的不匹配的最后一个元素,即37,38,39 ....所以在上面的例子中,我想要我的答案看起来像:
result = [(18,223,70,37)]
So as you can see in this case the algorithm would totally ignore the last number or 4th element while comparing individual tuples. Hence I want that if the first three elements of tuples are matched then it can predict that there is no difference even though fourth element may or may not be same.
因此,在这种情况下,您可以看到算法在比较单个元组时完全忽略最后一个数字或第四个元素。因此,我希望如果元组的前三个元素匹配,那么即使第四个元素可能相同也可能不相同,它可以预测没有差异。
What I tried:
我尝试了什么:
Apart from searching solutions I just wanted to try set(b_value)-set(a_value)
approach. But I knew it wont work as the result generated by this looks like this:
除了搜索解决方案,我只想尝试set(b_value)-set(a_value)方法。但我知道它不会起作用,因为这样生成的结果如下所示:
set([(18, 613, 70, 42), (18, 448, 70, 40), (18, 548, 70, 41), (18, 299, 70, 38), (18, 323, 70, 39), (18, 223, 70, 37)])
set([(18,613,70,42),(18,448,70,40),(18,548,70,41),(18,299,70,38),(18,323,70, 39),(18,223,70,37)])
This is pretty obvious result as the last element of all the tuples are not matching hence this method predicts all such tuples as unmatched.
这是非常明显的结果,因为所有元组的最后一个元素都不匹配,因此这个方法预测所有这些元组都是不匹配的。
EDIT: I would like to retain the value of 4th element in my result, so deleting all 4th element in all tuples and comparing is not an option.
编辑:我想在我的结果中保留第4个元素的值,因此删除所有元组中的所有第4个元素并进行比较不是一个选项。
1 个解决方案
#1
2
Produce a set for the first list containing the tuples without the last element; you can do a second loop over b
to collect those whose first elements are not in the set:
为包含没有最后一个元素的元组的第一个列表生成一个集合;你可以在b上做第二个循环来收集第一个元素不在集合中的那些:
a_set = {t[:-1] for t in a}
result = [t for t in b if t[:-1] not in a_set]
Algorithmically speaking, this does the same amount of work as converting both to sets and producing a set difference.
从算法上讲,这与将两者转换为集合并产生集合差异的工作量相同。
Demo:
>>> a = [(18, 299, 70, 33), (18, 323, 70, 34),
... (18, 448, 70, 35), (18, 548, 70, 36), (18, 613, 70, 37)]
>>> b = [(18, 223, 70, 37), (18, 299, 70, 38),
... (18, 323, 70, 39), (18, 448, 70, 40), (18, 548, 70, 41), (18, 613, 70, 42)]
>>> a_set = {t[:-1] for t in a}
>>> [t for t in b if t[:-1] not in a_set]
[(18, 223, 70, 37)]
#1
2
Produce a set for the first list containing the tuples without the last element; you can do a second loop over b
to collect those whose first elements are not in the set:
为包含没有最后一个元素的元组的第一个列表生成一个集合;你可以在b上做第二个循环来收集第一个元素不在集合中的那些:
a_set = {t[:-1] for t in a}
result = [t for t in b if t[:-1] not in a_set]
Algorithmically speaking, this does the same amount of work as converting both to sets and producing a set difference.
从算法上讲,这与将两者转换为集合并产生集合差异的工作量相同。
Demo:
>>> a = [(18, 299, 70, 33), (18, 323, 70, 34),
... (18, 448, 70, 35), (18, 548, 70, 36), (18, 613, 70, 37)]
>>> b = [(18, 223, 70, 37), (18, 299, 70, 38),
... (18, 323, 70, 39), (18, 448, 70, 40), (18, 548, 70, 41), (18, 613, 70, 42)]
>>> a_set = {t[:-1] for t in a}
>>> [t for t in b if t[:-1] not in a_set]
[(18, 223, 70, 37)]