如何在Python中找到单词旁边的单词 [英] How to find word next to a word in Python

查看:45
本文介绍了如何在Python中找到单词旁边的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Python 中找到一个词的出现并在这个词之后打印这个词.单词以空格分隔.

I would like to find an occurrence of a word in Python and print the word after this word. The words are space separated.

示例:

如果文件中出现单词sample"thisword".我想得到这个词.我想要一个正则表达式,因为 thisword 不断变化.

if there is an occurrence of the word "sample" "thisword" in a file . I want to get thisword. I want a regex as the thisword keeps on changing .

推荐答案

python 字符串有一个内置的 split 方法,可以将字符串拆分为由空格字符分隔的单词列表 (doc),它具有用于控制拆分单词方式的参数,然后您可以在列表中搜索你想要的词并返回下一个索引

python strings have a built in method split that splits the string into a list of words delimited by white space characters (doc), it has parameters for controlling the way it splits the word, you can then search the list for the word you want and return the next index

your_string = "This is a string"
list_of_words = your_string.split()
next_word = list_of_words[list_of_words.index(your_search_word) + 1]

这篇关于如何在Python中找到单词旁边的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆