Reputation: 77
Im having a problem to iterate between 2 lists to see if one of them has the elements of the other. Inside of one of those lists, I have another list. Sometimes this list have only strings, sometimes strings and.. another list.
So here is the problem. If It has strings only, I would like to know if ALL
of those strings are inside the Mainlist
.
But, If I have srtings AND
another list.. I would like to know if ALL
of the strings outside the list are in the Mainlist
and if AT LEAST ONE
string inside the list are in the Mainlist
.
So, lets say I have a Mainlist:
Mainlist = ['AA', 'BB', 'CC', 'DD', 'EE', 'FF']
Now, I have other list that I want to iterate over this Mainlist
:
list1 = [['AA', ['BB', 'YY']], ['AA', 'GG']]
On list1
I have two lists : ['AA', ['BB', 'YY']]
and ['AA', 'GG']
One has only strings and the other has strings and another list.
Now I need to know if the elements on list1
are on Mainlist
and get the index of the ones at list1
that give me a True
result.
In this case, it wolud be True
at index 0
. Because Mainlist
has items 'AA'
and 'BB'
.
other examples:
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
I am having a headache trying to achive that.. Does anyone has a hint that could help me with this? Thanks in advance
Upvotes: 2
Views: 82
Reputation: 6056
This should do it. You can find the explanations as comments:
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))
[True, False]
[False, True]
[False, False]
[False, True, False]
Upvotes: 1
Reputation: 6483
You could try this, returning a dictionary that has each index and its respective boolean:
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))
Ouput:
{0: False, 1: True}
{0: False, 1: False}
{0: False, 1: True, 2: False}
Upvotes: 2