python - 从python中的列表中搜索

标签 python

我正在尝试从 python 中的列表中进行搜索。每当我输入搜索词时,它总是说“没有推文包含搜索”,即使我知道该词在列表中也是如此。我做错了什么?

elif(ch==3):
        match = 0
        tweetlist.reverse()
        if tweetlist == []:
            print("There are no tweets to search.\n")
            continue
        else:
            search = input("What would you like to search for? ")
            for tweets in tweetlist:
                if(search in tweetlist):
                    match = 1
                if match == 1:
                    print ("Search Results")
                    print("----------")
                    print(tweets.get_author(), "-", tweets.get_age())
                    print(tweets.get_text(), "\n")
                elif match == 0:
                    print("No tweets contained,", search, "\n")

最佳答案

试试这个:

 ...
 matched_tweets = []
 if search in tweets.get_text(): 
     match = 1
     matched_tweets.append(tweets) # append all the matches to an array
 ...

 # to print out the results
 for tweet in matched_tweets:
     print(tweet.get_text(), "\n")

关于python - 从python中的列表中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34189893/

相关文章:

python - 如何根据该组中的行数分配列值

python - 为 pandas 数据框的一列着色 : change in format

python snap7 windows - 找不到 snap7 库

python - 选择单词出现后的子字符串

python - 通过按换行符拆分列,将 pandas 数据框分解为单独的行

python - 将 ifort 与 f2py 结合使用时禁用 fp-model strict

python - 为什么我的 GIMP Python 插件无法在我的 Ubuntu Linux Box 上运行?

python - 部署 Django : How do you do it?

python - int 的实例比其继承类的字节少得多

python - 属性错误 : module 'google.cloud.vision' has no attribute 'types'