简体   繁体   English

python:确定列表的所有条目是否在嵌套列表的条目内

[英]python: determine if all entries of a list are within entry of a nested list

I have a nested list such as: 我有一个嵌套列表,例如:

>>l = [[['bcsA', 'F'], ['bcsB', 'F'], ['bcsC', 'R']], [['bcsE', 'R'], ['bcsF', 'R']]]
>>n = [['GROUP A'], [[['bcsQ', 'F'], ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], ['bcsC', 'F']], [['yhjK', 'R']], [['yhjK', 'F']], [['bcsC', 'R'], ['bcsZ', 'R'], ['bcsB', 'R'], ['bcsA', 'R'], ['bcsQ', 'R']], [['bcsC', 'F'], ['bcsZ', 'F'], ['bcsB', 'F'], ['bcsA', 'F'], ['bcsQ', 'F']]]], 
      [[['GROUP A'], [[['bcsA', 'F'], ['bcsB', 'F'], ['bcsC', 'R']], [['bcsE', 'R'], ['bcsF', 'R']], [['yhjK', 'F']]]], 

So what I was hoping to do is if the nested lists [['bcsA', 'F'], ['bcsB', 'F'], ['bcsC', 'R']] and [['bcsE', 'R'], ['bcsF', 'R']] were found in the same list in the other nested list then it would give me the first entry. 所以我希望做的是嵌套列表[['bcsA', 'F'], ['bcsB', 'F'], ['bcsC', 'R']][['bcsE', 'R'], ['bcsF', 'R']]在另一个嵌套列表的同一列表中找到了[['bcsE', 'R'], ['bcsF', 'R']] ,那么它将给我第一个条目。

the output would, hopefully, be GROUP B . 希望输出是GROUP B

The list l will be different each time I go through the program. 每次我执行该程序时,列表l都会有所不同。

I have tried a bunch of variations of: 我尝试了以下几种变化:

>>>for x in n:
>>>    if all(e in x for e in l):
>>>         print n[n.index(x)][0] 

I think what it is doing is if the individual items in the nested list of l are anywhere in n then it will give me that they are in GROUP A but really the output should be GROUP B. I have tried it without the all() statement as well. 我认为它的作用是,如果l的嵌套列表中的单个项目在n中的任何位置,那么它将使我知道它们在GROUP A中,但实际上输出应为GROUPB。我已经尝试过使用all()声明。 I feel like this is a simple thing but I cannot figure it out. 我觉得这很简单,但我无法弄清楚。 I hope this makes sense. 我希望这是有道理的。 Thank you for any help! 感谢您的任何帮助!

tl;dr: How to tell if all values of a list are contained within a nested list and if so, print the first entry of the nested list. tl; dr:如何判断列表的所有值是否都包含在嵌套列表中,如果是,则打印嵌套列表的第一项。

我认为应该记住, bool([False]) == Truebool([True]) == True因此在使用结构[any(...)]: ,任何人都可以肯定地说这句话会是True

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

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