如何创建一个包含特定行业所有股票代码的列表?

huangapple go评论110阅读模式
英文:

How can i create a list full of all tickers of companies in a specific industry?

问题

我如何能够迭代遍历yfinance数据库并将特定行业的所有公司追加到特定的列表中?

  1. y = int(input("股票数量: "))
  2. def info(x):
  3. comp = yf.Ticker(x)
  4. print(comp.history(period="1d").head())
  5. print(comp.info['forwardPE'])
  6. print(comp.info['trailingPE'])
  7. print(comp.info['beta'])
  8. for a in range(y):
  9. info(x=input("输入股票代码: "))

到目前为止,这是我拥有的代码,它可以工作,我希望添加一些内容以便让我能够比较同一行业内的公司。此外,是否有可能使用的命令列表?我发现了一些命令,但使用它们时成功与否各不相同。

英文:

How would I be able to iterate through the yfinance database and append all companies within specific industries into specific lists?

  1. y = int(input("number of tickers:"))
  2. def info(x):
  3. comp = yf.Ticker(x)
  4. print(comp.history(period="1d").head())
  5. print(comp.info['forwardPE'])
  6. print(comp.info['trailingPE'])
  7. print(comp.info['beta'])
  8. for a in range(y):
  9. info(x=input("Enter ticker: "))

This is what I have so far, which works, and I was hoping to add something to allow me to compare companies within the same industry. Also, is there a list of possible commands that I can use? I've had varying success with ones I have found so far

答案1

得分: 0

在你的info()函数中,需要将公司信息打印出来的部分改为将股票代号追加到相应的行业列表中。

  1. import yfinance as yf
  2. y = int(input("Number of tickers: "))
  3. industry_lists = {}
  4. def info(x, industry):
  5. comp = yf.Ticker(x)
  6. print(comp.history(period="1d").head())
  7. print(comp.info['forwardPE'])
  8. print(comp.info['trailingPE'])
  9. print(comp.info['beta'])
  10. if industry in industry_lists:
  11. industry_lists[industry].append(x)
  12. else:
  13. industry_lists[industry] = [x]
  14. for a in range(y):
  15. ticker = input("Enter ticker: ")
  16. industry = input("Enter industry: ")
  17. info(x=ticker, industry=industry)
  18. for industry, tickers in industry_lists.items():
  19. print(f"Industry: {industry}")
  20. print("Tickers:", tickers)
  21. print()

要在同一行业内比较公司,您可以访问industry_lists字典中的行业列表,并执行所需的任何比较操作。

英文:

In your info() function, instead of printing the company's information, you need to append the ticker symbol to the corresponding industry list.

  1. import yfinance as yf
  2. y = int(input("Number of tickers: "))
  3. industry_lists = {}
  4. def info(x, industry):
  5. comp = yf.Ticker(x)
  6. print(comp.history(period="1d").head())
  7. print(comp.info['forwardPE'])
  8. print(comp.info['trailingPE'])
  9. print(comp.info['beta'])
  10. if industry in industry_lists:
  11. industry_lists[industry].append(x)
  12. else:
  13. industry_lists[industry] = [x]
  14. for a in range(y):
  15. ticker = input("Enter ticker: ")
  16. industry = input("Enter industry: ")
  17. info(x=ticker, industry=industry)
  18. for industry, tickers in industry_lists.items():
  19. print(f"Industry: {industry}")
  20. print("Tickers:", tickers)
  21. print()

To compare companies within the same industry, you can access the industry lists in the industry_lists dictionary and perform any comparisonsneeded.

huangapple
  • 本文由 发表于 2023年7月3日 03:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定