亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 列表為空但不應該為空

Python 列表為空但不應該為空

蕪湖不蕪 2023-07-18 10:35:37
一切都進展順利。只是,我想將命令應用于示例中的列表。test?=?[tweet.text?for?tweet?in?tweets]但它本質上返回一個空列表:print(test)。編輯:一:import syssys.modules[__name__].__dict__.clear()import osimport tweepy as twimport pandas as pdconsumer_key = 'xxx'consumer_secret = 'xxx'access_token = 'xxx'access_token_secret = 'xxx'auth = tw.OAuthHandler(consumer_key, consumer_secret)auth.set_access_token(access_token, access_token_secret)api = tw.API(auth, wait_on_rate_limit=True)# Define the search term and the date_since date as variablessearch_words = "#wildfire"date_since = "2020-01-09"#Collect tweetstweets = tw.Cursor(api.search,? ? q=search_words,? ?,? ? since=date_since).items(5)new_search = search_words + " -filter:retweets"#new_searchtweets = tw.Cursor(api.search,? ? ? ? ? ? ? ? ? ? ? ?q=new_search,? ? ? ? ? ? ? ? ? ? ? ?lang="en",? ? ? ? ? ? ? ? ? ? ? ?since=date_since).items(5)for tweet in tweets:? ? ? ?print(tweet.text)test = [tweet.text for tweet in tweets]print(test)一些評論表明推文可能是空的。如果我沒記錯的話,tweeps 不是空的,因為我可以循環內容。我想這很簡單,但任何幫助將不勝感激。
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

Cursor.items返回一個迭代器。你在這里迭代它:


for tweet in tweets:

? ?print(tweet.text)

讓它筋疲力盡。然后你嘗試再次迭代


test = [tweet.text for tweet in tweets]

但它已經沒有剩余的物品了。


首先列出列表,然后您就可以以可以迭代任意多次的形式獲得數據。


test = [tweet.text for tweet in tweets]

for text in test:

? ? print(text)


查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號