第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在for循環(huán)中調用列表項的數量

如何在for循環(huán)中調用列表項的數量

收到一只叮咚 2023-07-05 17:53:55
我正在編寫一個 for 循環(huán),從 pytrends 包中獲取 Google Trends?,F在我希望 for 循環(huán)為它在關鍵字列表中找到的每個關鍵字創(chuàng)建一個數據框。但我希望數據幀以列表項的編號而不是列表中的實際字符串命名。現在這是我的代碼:from pytrends.request import TrendReqpytrend = TrendReq(hl='de', tz=390, retries=10, backoff_factor=0.5)keywords = ['foo', 'bar', 'dummy']for keyword in keywords:  try:    pytrend.build_payload(      kw_list=[keyword],      geo='DE',      timeframe = 'now 1-d')    gbl = globals()    gbl['df_'+[str(i) for i in range(len(keywords))]] = pytrend.interest_over_time()    gbl['df_'+[str(i) for i in range(len(keywords))]] = gbl['df_'+[str(i) for i in range(len(keywords))]].drop(labels=['isPartial'],axis='columns')    print(keyword + ' was succesfully pulled from Google Trends')  except Exception as e:    print(keyword + ' was not successfully pulled because of the following error: ' + str(e))    continue但不幸的是這給了我以下錯誤:foo was not successfully pulled because of the following error: can only concatenate str (not "list") to strbar was not successfully pulled because of the following error: can only concatenate str (not "list") to strdummy was not successfully pulled because of the following error: can only concatenate str (not "list") to str所以,我的問題是,如何獲取列表中的項目編號來創(chuàng)建df_0、df_1、df_2等?謝謝!
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

問題在于將字符串添加到列表中。

您可能希望循環(huán)更加外部,并且您也可以使用enumarete這將為您提供一個同時包含項目及其編號的循環(huán)

for i, keyword in enumerate(keywords):
  gbl['df_'+str(i)] = ... something using keyword ...


查看完整回答
反對 回復 2023-07-05
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

from pytrends.request import TrendReq

pytrend = TrendReq(hl='de', tz=390, retries=10, backoff_factor=0.5)


keywords = ['foo', 'bar', 'dummy']


for keyword in keywords:

  try:

    pytrend.build_payload(

      kw_list=[keyword],

      geo='DE',

      timeframe = 'now 1-d')

    gbl = globals()

    for i in range(len(keywords)):

        gbl['df_'+str(i)] = pytrend.interest_over_time()

        gbl['df_'+str(i)] = gbl['df_'+str(i)].drop(labels=['isPartial'],axis='columns')

    print(keyword + ' was successfully pulled from Google Trends')

  except Exception as e:

    print(keyword + ' was not successfully pulled because of the following error: ' + str(e))

    continue

我對你的代碼做了一些更改并且它起作用了。


這是輸出。


foo was successfully pulled from Google Trends

bar was successfully pulled from Google Trends 

dummy was successfully pulled from Google Trends

上面代碼的問題是您無法將列表附加到字符串。


gbl['df_'+[str(i) for i in range(len(keywords))]] = pytrend.interest_over_time()

gbl['df_'+[str(i) for i in range(len(keywords))]] = gbl['df_'+[str(i) for i in range(len(keywords))]]




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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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