-1

I have the 2d list mainlist

mainlist = [['John','Doe',True],['Mary','Jane',False],['James','Smith',False]]
slist1 = ['John', 'Doe']
slist2 = ['John', 'Smith']
slist3 = ['Doe', 'John']
slist4 = ['John', True]

How to determine if a sublist of a sublist exists in a list where if slist1 is tested against mainlist will return True while slist2 will return False

I am thinking of something like this (code from here)

for sublist in mainlist:
    if all(i in sublist for i in slist1):
        return True
        break

is there a more "pythonic" way to do this? thanks

edit:

  1. slist1 tested against mainlist would return True
  2. slist2 would return False
  3. slist3 would return False
  4. slist4 would return False

so basically, i am just testing if slist is in the first 2 index of mainlist[x]

Community
  • 1
  • 1
JMY
  • 57
  • 1
  • 7
  • do you want to test all items in mainlist lists or just the 2 first ones? in which case I'd do `if slist1 == sublist[:2]:` instead of `all` – Jean-François Fabre Jan 15 '18 at 16:09
  • 1
    "How to determine if a sublist of a sublist exists in a list where if slist1 is tested against mainlist will return True while slist2 will return False" This makes my head hurt. Can you please give a clearer definition of what you want? – timgeb Jan 15 '18 at 16:10
  • what about making all `set`s – Jean-François Fabre Jan 15 '18 at 16:11
  • Do the boolean values in index 2 of each sublist of the mainlist *matter*? – Ma0 Jan 15 '18 at 16:13

3 Answers3

0

If i did understand your question i think you can use set() and intersection between sets, like this example:

def list_intersection(a, b):
   for sub in a:
       condition = set(sub) & set(b)
       condition2 = len(set(b)) == len(condition)
       if condition and condition2:
           return True
   return False


mainlist = [['John','Doe',True],['Mary','Jane',False],['James','Smith',False]]
slist1 = ['John', 'Doe']
slist2 = ['John', 'Smith']

list_intersection(mainlist, slist1)
# True
list_intersection(mainlist, slist2)
# False

PS: This solution have many weaknesses ... And it doesn't cover all the cases.

Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
0

Since OP did not respond, I am going to cover both cases.

If order does not matter; both ['John', 'Doe'] and ['Doe', 'John'] are assumed contained in mainlist:

def list_intersection_no_order(a, b):
    b = set(b)
    if any(b.difference(sublist) == set() for sublist in a):
        return True
    return False


mainlist = [['John','Doe',True],['Mary','Jane',False],['James','Smith',False]]
slist1 = ['John', 'Doe']
slist2 = ['John', 'Smith']
slist3 = ['Doe', 'John']

print(list_intersection_no_order(mainlist, slist1))
# True
print(list_intersection_no_order(mainlist, slist2))
# False
print(list_intersection_no_order(mainlist, slist3))
# True

If order does matter; ['John', 'Doe'] is contained but ['Doe', 'John'] is not in mainlist:

def list_intersection_with_order(a, b):
    if any(b == sublist[:2] for sublist in a):
        return True
    return False


mainlist = [['John','Doe',True],['Mary','Jane',False],['James','Smith',False]]
slist1 = ['John', 'Doe']
slist2 = ['John', 'Smith']
slist3 = ['Doe', 'John']

print(list_intersection_with_order(mainlist, slist1))
# True
print(list_intersection_with_order(mainlist, slist2))
# False
print(list_intersection_with_order(mainlist, slist3))
# False
Ma0
  • 15,057
  • 4
  • 35
  • 65
0

It seems you want to check if sublists in each list in mainlist has slist1 or slist2. In that case, you can do something simple like this:

def sublist_intersection(lst, sub):
    sub_len = len(sub)

    for l in lst:
        for i in range(0, len(l), sub_len):
            if l[i:i+sub_len] == sub:
                return True

    return False

or a shorter solution with any():

def sublist_intersection(lst, sub):
    sub_len = len(sub)
    return any(l[i:i+sub_len] == sub for l in lst for i in range(0, len(l), sub_len))

Which works as follows:

>>> mainlist = [['John','Doe',True],['Mary','Jane',False],['James','Smith',False]]
>>> slist1 = ['John', 'Doe']
>>> slist2 = ['John', 'Smith']
>>> slist3 = ['Doe', 'John']
>>> sublist_intersection(mainlist, slist1)
True
>>> sublist_intersection(mainlist, slist2)
False
>>> sublist_intersection(mainlist, slist3)
False

Note: This also assumes that you are checking contiguous sublists, where order does matter.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75