Determine if all elements in a list are present and in the same order in another list

后端 未结 10 1195
难免孤独
难免孤独 2021-01-02 15:35

How do I create a function sublist() that takes two lists, list1 and list2, and returns True if list1 is a s

10条回答
  •  天命终不由人
    2021-01-02 16:02

    def sublist(a, b):
        "if set_a is not subset of set_b then obvious answer is False"
        if not set(a).issubset(set(b)):
            return False
        n = 0
        for i in a:
            if i in b[n:]:
                "+1 is needed to skip consecutive duplicates, i.e. sublist([2,1,1],[2,1]) = False"
                "if +1 is absent then sublist([2,1,1],[2,1]) = True"
                "choose to use or not to use +1 according to your needs"
                n += b[n:].index(i) + 1
            else:
                return False
        return True
    

提交回复
热议问题