在lis中找到特定的单词序列

2024-09-28 03:21:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在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

Tags: theto字符串in列表序列listchinese
2条回答

您可以简单地遍历您的单词列表,并在每个索引中检查下列单词是否与您的任何序列匹配。你知道吗

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

您可以执行以下操作:

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。你知道吗

相关问题 更多 >

    热门问题