For 循环不遵守继续命令
For Loop Not Respecting Continue Command
我遇到的问题是 continue
命令不一致地跳过。它跳过数字输出 ebitda
但将不正确的 ticker
放在它旁边。为什么是这样?如果我将自动收报机设为 phm
它应该跳过的输入,它会正确地打印一个空列表 []
但是当无效的自动收报机放在有效的自动收报机旁边时,混乱就会开始发生。
import requests
ticker = ['aapl', 'phm', 'mmm']
ebitda = []
for i in ticker:
r_EV=requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/'+i+'?formatted=true&crumb=8ldhetOu7RJ&lang=en-US®ion=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
r_ebit = requests.get('https://query1.finance.yahoo.com/v10/finance/quoteSummary/' + i + '?formatted=true&crumb=B2JsfXH.lpf&lang=en-US®ion=US&modules=incomeStatementHistory%2CcashflowStatementHistory%2CbalanceSheetHistory%2CincomeStatementHistoryQuarterly%2CcashflowStatementHistoryQuarterly%2CbalanceSheetHistoryQuarterly%2Cearnings&corsDomain=finance.yahoo.com%27')
data = r_EV.json()
data1 = r_ebit.json()
if data1['quoteSummary']['result'][0]['balanceSheetHistoryQuarterly']['balanceSheetStatements'][0].get('totalCurrentAssets') == None:
continue #skips certain ticker if no Total Current Assets available (like for PHM)
ebitda_data = data['quoteSummary']['result'][0]['financialData']
ebitda_dict = ebitda_data['ebitda']
ebitda.append(ebitda_dict['raw']) #navigates to dictionairy where ebitda is stored
ebitda_formatted = dict(zip(ticker, ebitda))
print(ebitda_formatted)
# should print {'aapl': 73961996288, 'mmm': 8618000384}
# NOT: {'aapl': 73961996288, 'phm': 8618000384}
continue
工作得很好。您生成此列表:
[73961996288, 8618000384]
但是,您随后使用 ticker
压缩该列表,其中仍然有 3 个元素,包括 'phm'
。 zip()
当其中一个可迭代对象为空时停止,因此您生成以下元组:
>>> ebitda
[73961996288, 8618000384]
>>> ticker
['aapl', 'phm', 'mmm']
>>> zip(ticker, ebitda)
[('aapl', 73961996288), ('phm', 8618000384)]
如果您有选择地向列表中添加ebitda
个值,您还必须记录您处理的ticker
个值:
used_ticker.append(i)
并使用新列表。
或者你可以开始一个空的ebitda_formatted
字典并添加到循环中:
ebitda_formatted[i] = ebitda_dict['raw']
我遇到的问题是 continue
命令不一致地跳过。它跳过数字输出 ebitda
但将不正确的 ticker
放在它旁边。为什么是这样?如果我将自动收报机设为 phm
它应该跳过的输入,它会正确地打印一个空列表 []
但是当无效的自动收报机放在有效的自动收报机旁边时,混乱就会开始发生。
import requests
ticker = ['aapl', 'phm', 'mmm']
ebitda = []
for i in ticker:
r_EV=requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/'+i+'?formatted=true&crumb=8ldhetOu7RJ&lang=en-US®ion=US&modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&corsDomain=finance.yahoo.com')
r_ebit = requests.get('https://query1.finance.yahoo.com/v10/finance/quoteSummary/' + i + '?formatted=true&crumb=B2JsfXH.lpf&lang=en-US®ion=US&modules=incomeStatementHistory%2CcashflowStatementHistory%2CbalanceSheetHistory%2CincomeStatementHistoryQuarterly%2CcashflowStatementHistoryQuarterly%2CbalanceSheetHistoryQuarterly%2Cearnings&corsDomain=finance.yahoo.com%27')
data = r_EV.json()
data1 = r_ebit.json()
if data1['quoteSummary']['result'][0]['balanceSheetHistoryQuarterly']['balanceSheetStatements'][0].get('totalCurrentAssets') == None:
continue #skips certain ticker if no Total Current Assets available (like for PHM)
ebitda_data = data['quoteSummary']['result'][0]['financialData']
ebitda_dict = ebitda_data['ebitda']
ebitda.append(ebitda_dict['raw']) #navigates to dictionairy where ebitda is stored
ebitda_formatted = dict(zip(ticker, ebitda))
print(ebitda_formatted)
# should print {'aapl': 73961996288, 'mmm': 8618000384}
# NOT: {'aapl': 73961996288, 'phm': 8618000384}
continue
工作得很好。您生成此列表:
[73961996288, 8618000384]
但是,您随后使用 ticker
压缩该列表,其中仍然有 3 个元素,包括 'phm'
。 zip()
当其中一个可迭代对象为空时停止,因此您生成以下元组:
>>> ebitda
[73961996288, 8618000384]
>>> ticker
['aapl', 'phm', 'mmm']
>>> zip(ticker, ebitda)
[('aapl', 73961996288), ('phm', 8618000384)]
如果您有选择地向列表中添加ebitda
个值,您还必须记录您处理的ticker
个值:
used_ticker.append(i)
并使用新列表。
或者你可以开始一个空的ebitda_formatted
字典并添加到循环中:
ebitda_formatted[i] = ebitda_dict['raw']