How to get a sublist of a list between two words

前端 未结 2 1603
南笙
南笙 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:08

    This can be accomplished with the .index() method of lists, and with the slice notation..

    words = ['tree', 'water', 'dog', 'soap', 'cat', 'bird']
    start_index = words.index(start_word)
    end_index = words.index(end_word)
    sublist = words[start_index:end_index+1]
    

提交回复
热议问题