如何在Python中的字符串中查找确切单词的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38956274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find index of an exact word in a string in Python
提问by Khan
word = 'laugh'
string = 'This is laughing laugh'
index = string.find ( word )
index is 8, should be 17. I looked around hard, but could not find an answer.
指数是8,应该是17。我用力环顾四周,但找不到答案。
回答by DeepSpace
You should use regex (with word boundary) as str.find
returns the firstoccurrence. Then use the start
attribute of the match
object to get the starting index.
您应该使用正则表达式(带字边界)作为str.find
第一次出现的返回值。然后使用对象的start
属性match
来获取起始索引。
import re
string = 'This is laughing laugh'
a = re.search(r'\b(laugh)\b', string)
print(a.start())
>> 17
You can find more info on how it works here.
您可以在此处找到有关其工作原理的更多信息。
回答by miraculixx
Here is one approach without regular expressions:
这是一种没有正则表达式的方法:
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
This splits the string into words, finds the index of the matching word and then sums up the lengths and the blank char as a separater of all words before it.
这将字符串拆分为单词,找到匹配单词的索引,然后将长度和空白字符相加作为它之前所有单词的分隔符。
UpdateAnother approach is the following one-liner:
更新另一种方法是以下单行:
index = string.center(len(string) + 2, ' ').find(word.center(len(word) + 2, ' '))
Here both the string
and the word
are right and left padded with blanks as to capture the full word in any position of the string.
这里 thestring
和 theword
都是用空格填充的左右两边,以便在字符串的任何位置捕获完整的单词。
You should of course use regular expressions for performance and convenience. The equivalent using the re
module is as follows:
为了性能和方便,您当然应该使用正则表达式。使用该re
模块的等效项如下:
r = re.compile(r'\b%s\b' % word, re.I)
m = r.search(string)
index = m.start()
Here \b
means word boundary, see the re
documentation. Regex can be quite daunting. A great way to test and find regular expressions is using regex101.com
这里的\b
意思是word boundary,请参阅re
文档。正则表达式可能非常令人生畏。测试和查找正则表达式的一个好方法是使用regex101.com
回答by Daniel Lee
try this:
尝试这个:
word = 'laugh'
string = 'This is laughing laugh'.split(" ")
index = string.index(word)
This makes a list containing all the words and then searches for the relevant word. Then I guess you could add all of the lengths of the elements in the list less than index and find your index that way
这会生成一个包含所有单词的列表,然后搜索相关单词。然后我想你可以添加列表中小于索引的元素的所有长度,并以这种方式找到你的索引
position = 0
for i,word in enumerate(string):
position += (1 + len(word))
if i>=index:
break
print position
Hope this helps.
希望这可以帮助。
回答by XtrmJosh
Strings in code are not separated by spaces. If you want to find the space, you must include the space in the word you are searching for. You may find it would actually be more efficient for you to split the string into words then iterate, e.g:
代码中的字符串不以空格分隔。如果要查找空格,则必须在要搜索的单词中包含空格。您可能会发现将字符串拆分为单词然后进行迭代实际上会更有效,例如:
str = "This is a laughing laugh"
strList = str.split(" ")
for sWord in strList:
if sWord == "laugh":
DoStuff()
As you iterate you can add the length of the current word to an index and when you find the word, break from the loop. Don't forget to account for the spaces!
当您迭代时,您可以将当前单词的长度添加到索引中,当您找到该单词时,中断循环。不要忘记考虑空格!
回答by damilola sonaike
I stumbled upon this. I hope by now you would have figured it out. If you haven't maybe this would help. I had the same dilemma as you, was trying to print out a word using index.
我偶然发现了这一点。我希望现在你已经明白了。如果你还没有,也许这会有所帮助。我和你有同样的困境,试图用 index.html 打印出一个单词。
string = 'This is laughing laugh'
word = string.split(" ")
print(word[02])
This would print out laughing
.
这将打印出来laughing
。
I hope this helps. This is the first time of me answering a question on this forum, please pardon my syntax.
我希望这有帮助。这是我第一次在这个论坛上回答问题,请原谅我的语法。
Thank you.
谢谢你。