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

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

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

问题

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

y = int(input("股票数量: "))

def info(x):
    comp = yf.Ticker(x)
    print(comp.history(period="1d").head())
    print(comp.info['forwardPE'])
    print(comp.info['trailingPE'])
    print(comp.info['beta'])

for a in range(y):
    info(x=input("输入股票代码: "))

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

英文:

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

y = int(input("number of tickers:"))

def info(x):
    comp = yf.Ticker(x)
    print(comp.history(period="1d").head())
    print(comp.info['forwardPE'])
    print(comp.info['trailingPE'])
    print(comp.info['beta'])


for a in range(y):
    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()函数中,需要将公司信息打印出来的部分改为将股票代号追加到相应的行业列表中。

import yfinance as yf

y = int(input("Number of tickers: "))
industry_lists = {}

def info(x, industry):
    comp = yf.Ticker(x)
    print(comp.history(period="1d").head())
    print(comp.info['forwardPE'])
    print(comp.info['trailingPE'])
    print(comp.info['beta'])

    if industry in industry_lists:
        industry_lists[industry].append(x)
    else:
        industry_lists[industry] = [x]

for a in range(y):
    ticker = input("Enter ticker: ")
    industry = input("Enter industry: ")
    info(x=ticker, industry=industry)

for industry, tickers in industry_lists.items():
    print(f"Industry: {industry}")
    print("Tickers:", tickers)
    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.

import yfinance as yf

y = int(input("Number of tickers: "))
industry_lists = {}

def info(x, industry):
    comp = yf.Ticker(x)
    print(comp.history(period="1d").head())
    print(comp.info['forwardPE'])
    print(comp.info['trailingPE'])
    print(comp.info['beta'])
  
    if industry in industry_lists:
        industry_lists[industry].append(x)
    else:
        industry_lists[industry] = [x]

for a in range(y):
    ticker = input("Enter ticker: ")
    industry = input("Enter industry: ")
    info(x=ticker, industry=industry)

for industry, tickers in industry_lists.items():
    print(f"Industry: {industry}")
    print("Tickers:", tickers)
    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:

确定