python 如何从字符串中找到子字符串列表的位置?

0tdrvxhp  于 2023-03-11  发布在  Python
关注(0)|答案(4)|浏览(159)

如何从字符串中找到子字符串列表的位置?

给定一个字符串:
这架飞往圣彼得堡的飞机上周六从沙姆沙伊赫起飞仅23分钟后就在埃及西奈沙漠坠毁。
和子字符串列表:
[“The”、“plane”、“,”、“bound'、”for“、”St“、”Petersburg“、”,“、”crashed“、”in“、”Egypt“、”s”、“Sinai”、“desert”、“just'、”23“、”minutes“、”after“、”take-off“、”from“、”Sharm“、”el-Sheikh“、”on“、”Saturday“、”.“]
预期输出:

>>> s = "The plane, bound for St Petersburg, crashed in Egypt's Sinai desert just 23 minutes after take-off from Sharm el-Sheikh on Saturday."
>>> tokens = ['The', 'plane', ',', 'bound', 'for', 'St', 'Petersburg', ',', 'crashed', 'in', 'Egypt', "'s", 'Sinai', 'desert', 'just', '23', 'minutes', 'after', 'take-off', 'from', 'Sharm', 'el-Sheikh', 'on', 'Saturday', '.']
>>> find_offsets(tokens, s)
[(0, 3), (4, 9), (9, 10), (11, 16), (17, 20), (21, 23), (24, 34),
        (34, 35), (36, 43), (44, 46), (47, 52), (52, 54), (55, 60), (61, 67),
        (68, 72), (73, 75), (76, 83), (84, 89), (90, 98), (99, 103), (104, 109),
        (110, 119), (120, 122), (123, 131), (131, 132)]

解释输出,第一个子串“The”可以通过使用字符串s使用(start, end)索引找到。因此从所需的输出。
因此,如果我们循环遍历所需输出中的所有整数元组,我们将返回子字符串列表,即。

>>> [s[start:end] for start, end in out]
['The', 'plane', ',', 'bound', 'for', 'St', 'Petersburg', ',', 'crashed', 'in', 'Egypt', "'s", 'Sinai', 'desert', 'just', '23', 'minutes', 'after', 'take-off', 'from', 'Sharm', 'el-Sheikh', 'on', 'Saturday', '.']

我试过了

def find_offset(tokens, s):
    index = 0
    offsets = []
    for token in tokens:
        start = s[index:].index(token) + index
        index = start + len(token)
        offsets.append((start, index))
    return offsets

有没有其他方法可以从字符串中找到子字符串列表的位置?

58wvjzkj

58wvjzkj1#

第一个解决方案:

#use list comprehension and list.index function.
[tuple((s.index(e),s.index(e)+len(e))) for e in t]

第二个解决方案用于更正第一个解决方案中的问题:

def find_offsets(tokens, s):
    tid = [list(e) for e in tokens]
    i = 0
    for id_token,token in enumerate(tid):
        while (token[0]!=s[i]):            
            i+=1
        tid[id_token] = tuple((i,i+len(token)))
        i+=len(token)

    return tid

find_offsets(tokens, s)
Out[201]: 
[(0, 3),
 (4, 9),
 (9, 10),
 (11, 16),
 (17, 20),
 (21, 23),
 (24, 34),
 (34, 35),
 (36, 43),
 (44, 46),
 (47, 52),
 (52, 54),
 (55, 60),
 (61, 67),
 (68, 72),
 (73, 75),
 (76, 83),
 (84, 89),
 (90, 98),
 (99, 103),
 (104, 109),
 (110, 119),
 (120, 122),
 (123, 131),
 (131, 132)]   

#another test
s = 'The plane, plane'
t = ['The', 'plane', ',', 'plane']
find_offsets(t,s)
Out[212]: [(0, 3), (4, 9), (9, 10), (11, 16)]
wh6knrhe

wh6knrhe2#

import re

s = "The plane, bound for St Petersburg, crashed in Egypt's Sinai desert just 23 minutes after take-off from Sharm el-Sheikh on Saturday."
tokens = ['The', 'plane', ',', 'bound', 'for', 'St', 'Petersburg', ',', 'crashed', 'in', 'Egypt', "'s", 'Sinai', 'desert', 'just', '23', 'minutes', 'after', 'take-off', 'from', 'Sharm', 'el-Sheikh', 'on', 'Saturday', '.']

for token in tokens:
  pattern = re.compile(re.escape(token))
  print(pattern.search(s).span())

结果

(0, 3)
(4, 9)
(9, 10)
(11, 16)
(17, 20)
(21, 23)
(24, 34)
(9, 10)
(36, 43)
(44, 46)
(47, 52)
(52, 54)
(55, 60)
(61, 67)
(68, 72)
(73, 75)
(76, 83)
(84, 89)
(90, 98)
(99, 103)
(104, 109)
(110, 119)
(120, 122)
(123, 131)
(131, 132)
ffdz8vbo

ffdz8vbo3#

如果我们对子字符串一无所知,除了重新扫描整个文本以查找每个子字符串之外,别无他法。
如果像数据显示的那样,我们知道这些是文本的连续片段,按照文本顺序给出,那么在每次匹配后只扫描文本的“其余部分”就很容易了,但没有必要每次都剪切文本。

def spans(text, fragments):
    result = []
    point = 0  # Where we're in the text.
    for fragment in fragments:
        found_start = text.index(fragment, point)
        found_end = found_start + len(fragment)
        result.append((found_start, found_end))
        point = found_end
    return result

试验:

>>> spans('foo in bar', ['foo', 'in', 'bar'])
[(0, 3), (4, 6), (7, 10)]

这里假设每个片段都出现在文本的正确位置。您的输出格式没有提供不匹配报告的示例。使用.find代替.index可能会有所帮助,尽管只是部分帮助。

w7t8yxp5

w7t8yxp54#

def spans2(text, fragments):
    result = []
    for fragment in fragments:
        found_start = text.index(fragment)
        found_end = found_start + len(fragment)
        result.append((found_start, found_end))
    return(result)

这比下面的方法效果更好:

def spans(text, fragments):
    result = []
    point = 0  # Where we're in the text.
    for fragment in fragments:
        found_start = text.index(fragment, point)
        found_end = found_start + len(fragment)
        result.append((found_start, found_end))
        point = found_end
    return result

试验:

txt = "foo man bit"
frag = ["bit","man"]

spans2(txt,frag)

[(8, 11), (4, 7)]

spans(txt,frag)
---------------------------------------------------------------------------
ValueError

相关问题