[英]To check whether elements of a list is a subset of elements of another list
Here I am trying to check whether elements of 'b' that are [1,3],[1,5],[3,7],[5,7],[6,9],[8,10] are subsets of elements of 'a' that are [1,3,5,7] and [6,8,9,10]. 在这里,我试图检查'b'的元素是否为[1,3],[1,5],[3,7],[5,7],[6,9],[8,10] 'a'的元素的子集为[1、3、5、7]和[6、8、9、10]。 If any element of b is subset of any element of a then it gets removed from b.
如果b的任何元素是a的任何元素的子集,则将其从b中删除。
a = [[1, 3, 5, 7],[6, 8, 9, 10]]
b = [[1, 3], [1, 5],[10, 11],[6, 9],[8, 10]]
If any element of 'b' is subset of any element of 'a' then it gets removed from b. 如果“ b”的任何元素是“ a”的任何元素的子集,则将其从b中删除。 That means the new b should be :-
这意味着新的b应该是:
b = [[10,11]]
如果要进行集合操作,请使用python的集合数据类型,该数据类型具有issubset()
方法。
b = [x for x in b if not any(set(x).issubset(y) for y in a)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.