0

Basically for an assignment I need to determine if a list is a sublist of another list, which I saw was answered in this post Checking if list is a sublist, however there are some special requirements, that will be explained in the examples, that werent answered in that post. I also saw this post How to check if a list is in another list with the same order python which also didnt help determine the special requirements.

Example1:

list1 = 'a,q,b,q,q,q,q,q,q,c'
sublist = 'a,b,c'
output -> True

Explanation: Now, I know this isn't necessarily a sublist of list1, but a, b, and c are all present in list1, in the same order as the sublist variable, with only q separating them, which I can ignore because q isnt in sublist, which is why this would output true.

Example2:

list1 = 'b,q,q,q,q,a,q,c'
sublist = 'a,b,c'
output -> False

Explanation: Although this list does contain a, b, and c like the other example, it is out of order, hence why it would be false

Example3:

list1 = 'a,b,b,q,c'
sublist = 'a,b,c'
output -> False

Example4:

list1 = 'a,b,b,a,b,q,c'
sublist = 'a,b,c'
output -> True

Explanation: At the beginning of the list we have a,b,b which in the before explanation I stated was false, however following that part we have the correct order of a,b,c with a q separating the b and c

Here is my code. The problem I can't seem to find shows up in one hidden test case I have to run this through, I cant see the input or the output, which is making it hard to debug this. I've tried to run many of my own different test cases, but I can't seem to find what I'm missing here. I'm just wondering if anyone can figure out what I'm forgetting to take into account when running this.

sublist = 'a,b,c'.split(',')
l1 = 'a,q,b,a,q,q,q,q,q,q,q,q,q,c'.split(',')
item_order = []

for item in l1:
    #Check if I have found a sublist
    if item_order == sublist:
        break

    #If item is in sublist, but hasnt been seen yet
    elif item in sublist and item not in item_order:
        item_order.append(item)
        print('adding item', item_order)

    #If item has been seen and is in sublist
    elif item in item_order:
        #Reset and add duplicated item 
        item_order = []
        item_order.append(item)
        print('found duplicate, resetting list', item_order)

if item_order == sublist: print("true")
else: print("false")
Arxci
  • 95
  • 1
  • 7
  • downvoted, why? Did you not understand the question? – pk786 Feb 25 '20 at 01:55
  • It might of been confusing. Sorry if it is. I spent awhile trying to make it as clear as I can. – Arxci Feb 25 '20 at 01:56
  • Your code has a check for repeated items, but the description doesn't say how they're supposed to be handled. Neither of the examples demonstrates it. – Barmar Feb 25 '20 at 01:58
  • Is `a,b,b,q,c` supposed to be a match for `a,b,c`? – Barmar Feb 25 '20 at 02:00
  • Ok, so a,b,b,q,c will not be a match to a,b,c because the sublist a,b,c has only one b, while the list a,b,b,q,c has two. Which I why I have to do the check for duplicates – Arxci Feb 25 '20 at 02:04
  • So the gist of your question is: "There's something wrong with my code, but I don't know what it is. Please help me fix it." – martineau Feb 25 '20 at 02:06
  • Yes, I guess it can be summed up to that – Arxci Feb 25 '20 at 02:08

2 Answers2

3

You can filter the elements from list1 that doesn't exist in sublist and compare it with sublist, i.e.:

def good_seq(list1, sublist):
    return [x for x in list1.split(",") if x in sublist.split(",")] == sublist.split(",")

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

Can you try the following:

def dedupe(items):
    """Function to remove duplicate by preserving its order

    Args:
        items (list): the list  for which the duplicates must be removed

    Yields:
        generator: the generator with unique items
    """
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

def check_order(l1, sublist):
    l1 = list(dedupe(l1))
    result = [val for val in l1 if val in sublist] == sublist
    return result

sublist = 'a,b,c'.split(',')
l1 = 'a,q,b,a,q,q,q,q,q,q,q,q,q,c'.split(',')
print(check_order(l1, sublist))

sublist = 'a,b,c'.split(',')
l1 = 'b,q,q,q,q,a,q,c'.split(',')
print(check_order(l1, sublist))

Output:

True
False
Jeril
  • 7,858
  • 3
  • 52
  • 69