![](/img/trans.png)
[英]How do I create a nested dictionary to pair a dataframe's column's categories with its corresponding frequency of occurence?
[英]How do I correct my output so that it returns the specified categories with the corresponding tickers price sums inside the dictionary?
如何更正我的代碼,以便它對我指定的詞典(“垃圾”和“技術”)進行分類。
然后將每個代碼的每個類別的價格總和相加?
例如它會是 output:字典總和之后的類別字符串。
Trash:(AMC + DOGE-USD,GME 的價格總和)
技術:(TSLA + NIO + NVDA 的價格總和)
import requests
import yfinance as yf
portfolios = {'Trash': ['AMC', 'DOGE-USD', 'GME'], 'Tech':['TSLA','NIO','NVDA']}
for tickers in portfolios:
for tickers in portfolios[tickers]:
info = yf.Ticker(tickers).info
marketprice = str(info.get('regularMarketPrice'))
message= tickers +","+ marketprice
print(message)
#print (portfolios)
您需要像這樣修復循環:
for category in portfolios:
sum = 0
for ticker in portfolios[category]:
info = yf.Ticker(ticker).info
marketprice = info.get('regularMarketPrice')
sum += marketprice
message= ticker +","+ str(marketprice)
print(message)
print(category + ": " + sum)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.