I have two lists. One which has 10 lots of 3 objects(so calling as list[0]
, shows the 3 objects withing [0]). I then have another List that has a certain number of objects (it will be changing but we can use 6 objects as an example).
我有两个清单。一个有10个3个对象(所以调用list [0],显示3个对象[0])。然后我有另一个具有一定数量对象的List(它将会改变,但我们可以使用6个对象作为例子)。
I need to create a loop which tests the 6 objects against the 10 lots of 3 (So, list[0]
, list[1]
... etc) and if at any point the list of 6 has 3 objects which match to one of the 10 mini lists, it will Return True
.
我需要创建一个循环,测试6个对象对10个3的批次(所以,列表[0],列表[1] ...等),如果在任何点6的列表有3个对象匹配一个在10个迷你列表中,它将返回True。
As an example too:
作为一个例子:
6_ObjList = [A,B,D,H,G]
6_ObjList = [A,B,D,H,G]
3_ObjList[0] = [A,D,J]
- Should not return true and keep looping
3_ObjList [0] = [A,D,J] - 不应返回true并保持循环
3_ObjList[1] = [A,D,H]
- Should return true and break the loop
3_ObjList [1] = [A,D,H] - 应该返回true并打破循环
If none match - Return False
. Sorry if that is all jumbled up and makes no sense. This is kind of what I was thinking.
如果没有匹配 - 返回False。对不起,如果这一切都混乱了,没有任何意义。这就是我的想法。
for i in range(0,10):
if(6_ObjList.Contains(3_ObjList[i])):
return True
return False
3 个解决方案
#1
If I've correctly interpreted your question, you mean:
如果我正确解释了你的问题,你的意思是:
How can I find out whether one of the sublists of a nested list contains only items that are in another, separate list.
如何确定嵌套列表的其中一个子列表是否仅包含另一个单独列表中的项目。
In that case, you want something like:
在这种情况下,你需要这样的东西:
>>> lst_of_lst = [[1, 3, 7], [2, 4, 6], [0, 1, 2]]
>>> lst = [0, 1, 2, 3]
>>> any(all(item in lst for item in sub_lst) for sub_lst in lst_of_lst)
True
#2
if your items are unique, the perfomant way of doing this is by using sets:
如果您的项目是唯一的,那么执行此操作的方法是使用集合:
list6 = set(['A', 'B', 'D', 'H', 'G'])
list10 = [set(['A', 'D', 'J']),
set(['A', 'D', 'H'])]
def myfunc():
for i in list10:
if i.issubset(list6):
return True
return False
#3
for i in 6_ObjList:
for j in 3_ObjList:
if i == j:
return True
else:
return False
#1
If I've correctly interpreted your question, you mean:
如果我正确解释了你的问题,你的意思是:
How can I find out whether one of the sublists of a nested list contains only items that are in another, separate list.
如何确定嵌套列表的其中一个子列表是否仅包含另一个单独列表中的项目。
In that case, you want something like:
在这种情况下,你需要这样的东西:
>>> lst_of_lst = [[1, 3, 7], [2, 4, 6], [0, 1, 2]]
>>> lst = [0, 1, 2, 3]
>>> any(all(item in lst for item in sub_lst) for sub_lst in lst_of_lst)
True
#2
if your items are unique, the perfomant way of doing this is by using sets:
如果您的项目是唯一的,那么执行此操作的方法是使用集合:
list6 = set(['A', 'B', 'D', 'H', 'G'])
list10 = [set(['A', 'D', 'J']),
set(['A', 'D', 'H'])]
def myfunc():
for i in list10:
if i.issubset(list6):
return True
return False
#3
for i in 6_ObjList:
for j in 3_ObjList:
if i == j:
return True
else:
return False