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

后端 未结 10 1211
难免孤独
难免孤独 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:05

    here is a better solution using regex:

    import re
    
    
    def exiSt(short,long):
        r = ''.join(["(.*"+str[x]+")" for x in short])
        return re.match(r,','.join([str(x) for x in long])) == None
    
    long = [1, 2, 3, 4, 5]
    short1 = [1,2,5]
    short2 = [1,5,3]
    
    exiSt(short1,long)
    >> True
    
    exiSt(short2,long)
    >> False
    

提交回复
热议问题