【问题标题】:Trouble with creating list创建列表有问题
【发布时间】:2019-06-14 22:32:01
【问题描述】:

我制作了一个股票筛选器,提供符合我标准的股票的技术数据,最后,我希望它打印一份符合我标准的股票的简明清单。我尝试使用 append 函数,但它只打印一只股票,相反,我希望它打印每只股票的股票代码,例如'mmm',...

当前输出如下所示。 嗯 追踪市盈率:17.61 股本回报率:54.34% 收入:32.35B 季度收入增长:-5.00%


def scrape(stock_list, interested, technicals):
    for each_stock in stock_list:
        technicals = scrape_yahoo(each_stock)
        condition_1 = float(technicals.get('Return on Equity',0).replace('%','').replace('N/A','-100')) > 25
        condition_2 = float(technicals.get('Trailing P/E',0).replace('N/A','')) > 15
        condition_3 = float(technicals.get('Price/Book (mrq)',0)) <15
        if (condition_1 and condition_2)==True:
            print(each_stock)
            SuggestedStocks = []
            SuggestedStocks.append(each_stock)  
            for ind in interested: 

                print(ind + ": "+ technicals[ind])         
            print("------")
            time.sleep(1)                                                    # Use delay to avoid getting flagged as bot
    #return technicals
    print(SuggestedStocks)


def main():

    stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD']
    interested = ['Trailing P/E', 'Return on Equity', 'Revenue', 'Quarterly Revenue Growth']
    technicals = {}

    tech = scrape(stock_list, interested, technicals)
    print(tech)
main()

它只打印符合我条件的最后一只股票的代码:ADBE。

【问题讨论】:

  • 为什么在append()之前的循环内将SuggestedStocks初始化为一个空列表?
  • 我没有投反对票 - 请停止做出不真实的假设并继续讨论代码。例如if (condition_1 and condition_2)==True:,可能更像是if condition_1 and condition_2: - 快乐编码

标签: python python-3.x list finance


【解决方案1】:

这是因为您在每个循环SuggestedStocks = [] 中重置了建议股票,您应该在循环之外对其进行初始化。

【讨论】:

  • 谢谢!我以后会注意那个的!
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 2019-12-25
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多