我需要在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
答案 0 :(得分:1)
您可以简单地遍历单词列表,并检查每个索引是否跟随单词匹配您的任何序列。
words = ['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"
sequences = {'seq{}'.format(idx): i.split() for idx, i in enumerate([seq0, seq1, seq2])}
for idx in range(len(words)):
for k, v in sequences.items():
if idx + len(v) < len(words) and words[idx: idx+len(v)] == v:
print(k, idx)
输出:
seq1 6
seq0 10
seq2 15
答案 1 :(得分:0)
您可以执行以下操作:
def find_sequence(seq, _list):
seq_list = seq.split()
all_occurrence = [idx for idx in [i for i, x in enumerate(_list) if x == seq_list[0]] if seq_list == list_[idx:idx+len(seq_list)]]
return -1 if not all_occurrence else all_occurrence[0]
输出:
for seq in [seq0, seq1, seq2]:
print(find_sequence(seq, list_))
10
6
15
注意,如果找不到序列,您将得到 -1 。