我有一个words文件来查找它们在文本中的一致性(最多3个位置左右)
文件字数:
buy
time
glass
home
red
文本文件:
After almost a decade selling groceries online, Amazon has failed to make a major dent on its own as consumers have shown a stubborn urge to buy items like fruits, vegetables and meat in person. Do you find yourself spending more time on your phone than you ... mindlessly passing time on a regular basis by staring at your phone? The process can be fraught with anxiety, as many different glass styles are available, and points of view clash on what is proper and necessary
脚本:
def keywordsContext(file, fileName):
#file: text file
#fileName: words file
with open(file, "r") as f, open(fileName, "r") as fi:
corpus = f.read().split()
pivot = fi.read().split()
for keywords in pivot:
if keywords in corpus:
index = pivot.index(keywords)
contexts = keywords+":", pivot[index-3:index], pivot[index+1:index+4]
print(contexts)
else:
pass
输出:
('buy:', [], ['time', 'glass', 'home'])
('time:', [], ['glass', 'home', 'red'])
('glass:', [], ['home', 'red'])
None
我想要的输出:
^{4磅}$编辑
还有。。。如果同一个词出现不止一次?我做了一个测试(在语料库中再放一句话来重复“玻璃”这个词)。我试着放一段时间(语料库)!=0,但它是一个循环,重复使用相同的输出。。。在
^{pr2}$输出:
buy: stubborn urge to buy items like fruits,
time: yourself spending more time on your phone
glass: as many different glass styles are available,
buy: stubborn urge to buy items like fruits,
time: yourself spending more time on your phone
glass: as many different glass styles are available,
buy: stubborn urge to buy items like fruits,
time: yourself spending more time on your phone
glass: as many different glass styles are available,
...
输出
^{pr2}$列表名称有一些错误。试试看:
编辑:根据OP edit,此程序打印所有出现的word
输出:
^{pr2}$相关问题 更多 >
编程相关推荐