我需要在python中找到列表中特定序列(字符串序列)的起始索引。你知道吗
例如
list = ['In', 'a', 'gesture', 'sure', 'to', 'rattle', 'the', 'Chinese', 'Government', ',', 'Steven', 'Spielberg', 'pulled', 'out', 'of', 'the', 'Beijing', 'Olympics', 'to', 'protest', 'against', 'China', '_s', 'backing', 'for', 'Sudan', '_s', 'policy', 'in', 'Darfur', '.']
例如
seq0 = "Steven Spielberg"
seq1 = "the Chinese Government"
seq2 = "the Beijing Olympics"
输出应如下所示:
10
6
15
您可以简单地遍历您的单词列表,并在每个索引中检查下列单词是否与您的任何序列匹配。你知道吗
输出:
您可以执行以下操作:
输出:
注意,如果找不到序列,您将得到-1。你知道吗
相关问题 更多 >
编程相关推荐