简体   繁体   English

for 循环只返回最后一个值

[英]for loop only returns the last value

I am new to python and I am doing a project on fetching tweets.我是 python 的新手,我正在做一个关于获取推文的项目。 I was able to do so and now I want to get the hashtags from all those tweets.我能够这样做,现在我想从所有这些推文中获取主题标签。 But when I tried to do a for loop in order to append my list, it only captures the last item/values.但是当我尝试执行 for 循环以附加我的列表时,它只捕获最后一个项目/值。 I've been all over google and even tried chatGPT but I could not seem to get it right.我到处都是谷歌,甚至尝试过 chatGPT,但我似乎无法正确使用。

Here is my code if anyone is kind to please look and see what I am missing.这是我的代码,如果有人愿意请看一下我缺少的东西。



keyword = str(input("Enter hashtag"))
date = str(input("Enter date: YYYY-MM-DD"))

tweets = tweepy.Cursor(api.search_tweets, q=keyword, since_id=date, lang='en', tweet_mode = "extended",).items(30) # this is correct

#created columns for Dataframe
columns = ['Date & Time', 'User', 'Retweet_no' ,'Text', 'Location'] 
data_la= [] 

#Created for loop to append data_la 
for tweet in tweets:
  data_la.append([tweet.created_at, tweet.user.screen_name, tweet.retweet_count, tweet.full_text, tweet.geo])
print(tweet.full_text)  
## trying to get hashtags of tweets fetched. 
#Get the tweet text
  tweet_text = tweet.full_text
#Use a regex to find all hashtags in the tweet text
  hashtags = re.findall(r"#(\w+)", tweet_text)
  print('items:', hashtags)
  # Use the entities attribute to get the hashtags

hashtag_list = []
for word in tweets:
     if word == '#':
       hashtag_list.append(word)
print('List:', hashtag_list)

I've been everywhere on google trying to find the answer but to no avail.我到处都在谷歌上试图找到答案,但无济于事。 I have been stuck with this problem for over a week now.我已经被这个问题困扰了一个多星期了。

As RedzMakersError said, you only check if the word is # , and only # .正如 RedzMakersError 所说,你只检查这个词是否是# ,并且只检查#

You should try something like:你应该尝试这样的事情:

 if word.startswith('#'):
   hashtag_list.append(word)

As the name of the function says, it returns True if the string starts with # , False otherwise.正如函数名称所说,如果字符串以#开头,则返回True ,否则返回False

Official documentation: https://docs.python.org/3/library/stdtypes.html#str.startswith官方文档: https ://docs.python.org/3/library/stdtypes.html#str.startswith

I think your problem is that with this line:我认为你的问题是这条线:

if word == '#':

you check if the word is only a "#"你检查这个词是否只是一个“#”

You probaly just want to check if the word starts with a hashtag character (as hashtags do).您可能只想检查单词是否以主题标签字符开头(就像主题标签一样)。 You can do that with the startswith() function wich checks if a string starts with the given character and returns true if it does.您可以使用startswith()函数来做到这一点,该函数检查字符串是否以给定字符开头,如果是则返回true

So in youre case your code should probably look like this:因此,在您的情况下,您的代码可能应该如下所示:

if word.startswith("#"):
    hashtag_list.append(word)

Here you can read a bit more about startswith() : https://www.w3schools.com/python/ref_string_startswith.asp在这里您可以阅读更多关于startswith()的信息: https ://www.w3schools.com/python/ref_string_startswith.asp

hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM