我正在尝试编写一个算法,用Python在一个大文本文件中找到一个包含不同行的单词的短语
文件内容如下
fkerghiohgeoihhgergerig ooetbjoptj
enbotobjeob hi how
are you lerjgoegjepogjejgpgrg]
ekrngeigoieghetghehtigehtgiethg
ieogetigheihietipgietigeitgegitie
.......
在这种情况下,算法应该搜索短语“hi how are you”,并返回True。
由于文件可能很大,因此无法一次读取所有文件内容
Tags:
您可以一次读取一个字符的文件,并将换行符更改为空格。那么,这只是一个列出通缉犯名单的问题
如果您想对空格和大小写敏感度更开放一些,可以在比较之前删除所有空格和小写
这里有一种解决问题的方法:
编辑:
如果是“相当大”的文件,则按顺序访问这些行,不要将整个文件读入内存:
编辑:
由于字符串中的单词可以位于连续的行上,因此您需要使用计数器来跟踪迭代的单词。比如说,
文本文件
它给出了
here
的输出,因为找到了连续的单词相关问题 更多 >
编程相关推荐