【问题标题】:How to find index of an exact word in a string in Python [duplicate]如何在Python中查找字符串中确切单词的索引[重复]
【发布时间】:2016-12-21 17:30:05
【问题描述】:
word = 'laugh'    
string = 'This is laughing laugh'
index = string.find ( word )

索引是 8,应该是 17。 我用力环顾四周,但找不到答案。

【问题讨论】:

  • Python 新手,re 太复杂了,我无法解决这个问题!
  • 当我搜索“如何在字符串中查找单词”时,我在这个网站上发现了 194 个问题。你是说这些答案没有有帮助吗?
  • 8 为正确答案,find 返回第一个匹配子串的起始位置
  • 这能回答你的问题吗? Finding the position of a word in a string

标签: python find word


【解决方案1】:

您应该使用正则表达式(带有单词边界),因为 str.find 返回 first 出现。然后使用match对象的start属性获取起始索引。

import re

string = 'This is laughing laugh'

a = re.search(r'\b(laugh)\b', string)
print(a.start())
>> 17

你可以找到更多关于它是如何工作的信息here

【讨论】:

  • 太棒了!你能告诉我如何在 re 表达式中使用变量,即我想用 word 代替(笑)?
  • @Khan 就像使用任何 Python 字符串一样。可以连接或使用.format,即word = 'laugh' ; re.search(r'\b({})\b'.format(word), string)
  • 这有效:re.compile(r'\b%s\b' % word, re.I) 不确定为什么 re.search(r'\b({})\b‌​' .format(word), string) 没有...
  • 非常感谢!花了很多时间来找出答案(新手!)。
【解决方案2】:

试试这个:

word = 'laugh'    
string = 'This is laughing laugh'.split(" ")
index = string.index(word)

这会创建一个包含所有单词的列表,然后搜索相关单词。然后我猜你可以添加列表中所有元素的长度小于索引并以这种方式找到你的索引

position = 0
for i,word in enumerate(string):
    position += (1 + len(word))
    if i>=index:
        break

print position  

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    这是一种不使用正则表达式的方法:

    word = 'laugh'    
    string = 'This is laughing laugh'
    # we want to find this >>> -----
    # index   0123456789012345678901     
    words = string.split(' ')
    word_index = words.index(word)
    index = sum(len(x) + 1 for i, x in enumerate(words) 
                if i < word_index) 
    => 17
    

    这会将字符串拆分为单词,找到匹配单词的索引,然后将长度和空白字符相加,作为它之前所有单词的分隔符。

    更新另一种方法是以下单行:

    index = string.center(len(string) + 2, ' ').find(word.center(len(word) + 2, ' '))
    

    这里stringword 左右都用空格填充,以便在字符串的任何位置捕获完整的单词。

    您当然应该使用正则表达式来提高性能和方便性。使用re 模块的等价物如下:

    r = re.compile(r'\b%s\b' % word, re.I)
    m = r.search(string)
    index = m.start()
    

    这里\b 表示单词边界,请参阅re 文档。正则表达式可能非常令人生畏。测试和查找正则表达式的好方法是使用regex101.com

    【讨论】:

    • 投反对票,但请添加评论,以便我改进答案。
    • r = re.compile(r'\b%s\b' % word, re.I) 就像一个魅力。您的完整解决方案也有效!非常感谢!
    • 投反对票的原因是这个答案(它的两个部分)已经以非常相似的形式存在。
    • @XtrmJosh 我自己想出了这些解决方案和整个答案。另外,如果你仔细看,这个确切的解决方案不是由其他人发布的。
    • index = sum(len(x) + 1 for i, x in enumerate(words) if i
    【解决方案4】:

    代码中的字符串不使用空格分隔。如果要查找空格,则必须在要搜索的单词中包含空格。您可能会发现将字符串拆分为单词然后进行迭代实际上会更有效,例如:

    str = "This is a laughing laugh"
    strList = str.split(" ")
    for sWord in strList:
        if sWord == "laugh":
            DoStuff()
    

    当您迭代时,您可以将当前单词的长度添加到索引中,当您找到该单词时,退出循环。不要忘记考虑空格!

    【讨论】:

    • 我可以发现这个词在字符串中,我想知道它的索引。
    • 我的错,您可以在迭代时添加每个单词的长度。它可能比列出的正则表达式方法效率低,但我尽量避免在 Python 中使用正则表达式 - 我将其视为一种脚本语言,并且是一种易于阅读的东西。
    【解决方案5】:

    我偶然发现了这一点。我希望现在你已经明白了。如果你没有,也许这会有所帮助。我和你有同样的困境,试图用索引打印出一个单词。

    string = 'This is laughing laugh'
    word = string.split(" ")
    print(word[02])
    

    这将打印出laughing

    我希望这会有所帮助。这是我第一次在这个论坛上回答问题,请原谅我的语法。

    谢谢。

    【讨论】:

    • print(word[02]) 这将在 Python 3 中失败:“SyntaxError: 十进制整数文字中的前导零是不允许的”
    猜你喜欢
    • 2021-06-30
    • 2018-11-20
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多