在python中从列表中搜索

2024-06-16 13:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从python的列表中搜索。每当我输入搜索词时,它总是说“No tweets containedsearch”,即使我知道这个词在列表中。我做错什么了?你知道吗

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")

Tags: tonoin列表forsearchgetif
1条回答
网友
1楼 · 发布于 2024-06-16 13:36:40

试试这个:

 ...
 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")

相关问题 更多 >