Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何确定在.txt文件中是否找到列表中的任何元素?_Python_Arrays_Tweepy - Fatal编程技术网

Python 如何确定在.txt文件中是否找到列表中的任何元素?

Python 如何确定在.txt文件中是否找到列表中的任何元素?,python,arrays,tweepy,Python,Arrays,Tweepy,我已经通过Tweety导入了我的商业对手的时间表。 我想搜索从文件中导入的一些关键字 f = open('base.txt','r') with open('base.txt') as f: content = [f.read()] 我想知道他是否使用了我的关键词列表中的任何单词。 你能帮我吗 据我所知,由于你的问题所提供的信息很少, mmap图书馆为您的问题服务 import mmap with open('test.txt', 'rb', 0) as file, mmap.mma

我已经通过Tweety导入了我的商业对手的时间表。 我想搜索从文件中导入的一些关键字

f = open('base.txt','r')
with open('base.txt') as f:
    content = [f.read()]
我想知道他是否使用了我的关键词列表中的任何单词。
你能帮我吗

据我所知,由于你的问题所提供的信息很少, mmap图书馆为您的问题服务

import mmap

with open('test.txt', 'rb', 0) as file, mmap.mmap(file.fileno(), 0, 
access=mmap.ACCESS_READ) as s:
# test.txt has a sentence "where am i? I am here!"
    keywords = ["ie","ame!","here"]
    for idx,keyword in enumerate(keywords):
        if s.find(bytes(keyword,encoding ="utf8")) != -1:
            print('True')
            print("Keyword is" ,keyword)
        else:
            print('Nothing')
# Nothing
# Nothing
# True
# Keyword is here

循环浏览单词列表,检查每个单词是否出现在其他列表中

for word in list:
    if word in other_list:
        print(word)

如果变量中的内容:print(“正常”)或者:print(“无”),我认为@tglaria正在寻找您已经尝试过的代码。另外,一些示例数据也会有所帮助。您搜索的关键字是intersection.mcf=api.user_timeline(id=“georgehwbush”)mcf=str(mcf)print(mcf)[Status(contributors=None,truncated=True,text=u',在我的例子中,成为最老总统的几件好事之一是intersection:
set(mine)&set(his)
谢谢!我不能给你们投票(我在这里对young说),但你们可以在壁炉里感受到投票的力量;)很棒,要知道有些词像“这里”、“哪里”、“某处”和“哪里”可能会返回正数。因此,必须对代码进行调整,以返回txt文件中所有正数的实际单词,以避免误报。嗯,为什么在这里使用memmap?优点是什么?@juanpa.arrivillaga Good question:)是我在使用它之前提到的。这并不能真正回答我的问题。你认为这里的优点是什么?为什么不逐行迭代文件?你真的不需要随机访问文件…我认为问题是在.txt文件中而不是在另一个列表中查找单词。问题编辑错误。当你查看@Kamil中的示例代码时,很明显,他是在尝试从.txt文件而不是从另一个列表中查找单词。