How to get a sublist of a list between two words

前端 未结 2 1592
南笙
南笙 2020-12-10 23:57

Starting from a list like this:

words = [\'tree\', \'water\', \'dog\', \'soap\', \'bike\', \'cat\', \'bird\']

I want to get the sublist bet

2条回答
  •  时光说笑
    2020-12-11 00:12

    def sublist_two_words(array, start_word, end_word):
        result = []
        for word in array:
            if result or word == start_word:
                result.push(word)
            if word == end_word:
                break
        return result
    

    This way even if there is not end_word it gets the whole remaining list. If I got correctly your task.

提交回复
热议问题