在其他列表中迭代python列表
我在两个列表之间进行迭代以查看其中一个列表是否包含另一个列表的元素时遇到了问题。在其中一个列表中,我有另一个列表。有时此列表仅包含字符串,有时包含字符串和。。另一份名单 这就是问题所在。如果它只有字符串,我想知道那些字符串中的在其他列表中迭代python列表,python,Python,我在两个列表之间进行迭代以查看其中一个列表是否包含另一个列表的元素时遇到了问题。在其中一个列表中,我有另一个列表。有时此列表仅包含字符串,有时包含字符串和。。另一份名单 这就是问题所在。如果它只有字符串,我想知道那些字符串中的是否都在Mainlist中 但是,如果我有srtings和另一个列表。。我想知道列表外的所有字符串是否都在Mainlist中,列表内的至少一个字符串是否在Mainlist中 假设我有一个主要列表: Mainlist=['AA','BB','CC','DD','EE','F
是否都在Mainlist
中
但是,如果我有srtings和
另一个列表。。我想知道列表外的所有字符串是否都在Mainlist
中,列表内的至少一个字符串是否在Mainlist
中
假设我有一个主要列表:
Mainlist=['AA','BB','CC','DD','EE','FF']
现在,我有另外一个列表,我想在这个Mainlist
上迭代:
list1=[['AA'、['BB'、['YY']]、['AA'、['GG']]]
在list1
上,我有两个列表:['AA',['BB','YY']]
和['AA','GG']
一个只有字符串,另一个有字符串和另一个列表
现在我需要知道list1
上的元素是否在Mainlist
上,并获取list1
上的元素的索引,这些元素给出了True
结果
在这种情况下,在索引0
处应该是True
。因为Mainlist
中有'AA'
和'BB'
项
其他例子:
list2 = [['AA', 'JJ'], ['EE', 'FF']] # False at index 0 = Mainlist does not have 'JJ' / True at index 1 = Mainlist has 'EE' and 'FF'
list3 = [['AA', 'KK', ['BB', 'CC']], ['JJ', 'GG']] # False at index 0 item 'KK' is not in Mainlist / False at index 1 = Mainlist does not have 'JJ' and 'GG'
list4 = [['EE', 'FF', "OO"], ['AA', 'BB', 'CC',['DD', 'MM']], ['GG', ['BB', 'CC', 'DD']]] #False at index 0 = Mainlist does not have 'OO' / True at index 1 = Even if 'MM' is not in Mainlist, 'DD' is. / False at index 2 = 'GG' is not in Mainlist
为了达到这个目的,我头疼。。有没有人能给我一个提示?
提前感谢这应该可以。您可以在注释中找到解释:
list1 = [['AA', ['BB', 'YY']], ['AA', 'GG']] # True, False
list2 = [['AA', 'JJ'], ['EE', 'FF']] # False at index 0 = Mainlist does not have 'JJ' / True at index 1 = Mainlist has 'EE' and 'FF'
list3 = [['AA', 'KK', ['BB', 'CC']], ['JJ', 'GG']] # False at index 0 item 'KK' is not in Mainlist / False at index 1 = Mainlist does not have 'JJ' and 'GG'
list4 = [['EE', 'FF', "OO"], ['AA', 'BB', 'CC', ['DD', 'MM']], ['GG', ['BB', 'CC', 'DD']]] # False at index 0 = Mainlist does not have 'OO' / True at index 1 = Even if 'MM' is not in Mainlist, 'DD' is. / False at index 2 = 'GG' is not in Mainlist
Mainlist = ['AA', 'BB', 'CC', 'DD', 'EE', 'FF']
def eval_list(lst):
r = []
for sublist in lst:
if all(isinstance(i, str) for i in sublist): # if everything is str
r.append(all(i in Mainlist for i in sublist)) # check if all of them in Mainlist
else: # else
r.append(
all(isinstance(item, str) and item in Mainlist # check if item is in Mainlist if it is str
or # or
any(i in Mainlist for i in item) # check any i in item is in Mainlist
for item in sublist) # for every item in sublist
)
return r
for lst in [list1, list2, list3, list4]:
print(eval_list(lst))
您可以尝试这样做,返回一个包含每个索引及其相应布尔值的字典:
list2 = [['AA', 'JJ'], ['EE', 'FF']] # False at index 0 = Mainlist does not have 'JJ' / True at index 1 = Mainlist has 'EE' and 'FF'
list3 = [['AA', 'KK', ['BB', 'CC']], ['JJ', 'GG']] # False at index 0 item 'KK' is not in Mainlist / False at index 1 = Mainlist does not have 'JJ' and 'GG'
list4 = [['EE', 'FF', "OO"], ['AA', 'BB', 'CC',['DD', 'MM']], ['GG', ['BB', 'CC', 'DD']]] #False at index 0 = Mainlist
Mainlist = ['AA', 'BB', 'CC', 'DD', 'EE', 'FF']
def func(ls):
dct={}
for idx,l in enumerate(ls):
if all(isinstance(elem, str) for elem in l):
dct.update({idx:all(elem in Mainlist for elem in l)})
else:
cond=all(el in Mainlist for el in [i for i in l if isinstance(i, str)]) and any(ele in Mainlist for el in [i for i in l if isinstance(i, list)] for ele in el)
dct.update({idx:cond})
return dct
print(func(list2))
print(func(list3))
print(func(list4))
输出:
{0: False, 1: True}
{0: False, 1: False}
{0: False, 1: True, 2: False}
你提到:1。当列表仅包含字符串2时所需的内容。当列表包含字符串和其他列表时所需的内容。当它只有其他列表时,期望得到什么?
{0: False, 1: True}
{0: False, 1: False}
{0: False, 1: True, 2: False}