如何在字典中提取信息、排序和切片 (python3),其中值是列表

How to extract info, sort and slice in a dictionary (python3) where values are lists

提问人:LennyB 提问时间:3/13/2022 更新时间:3/13/2022 访问量:54

问:

项目是对多个交易所的派息股票进行识别、排名和分类。

简而言之,程序流程(使用 python 3.8)如下:

第 1 阶段 - 从价差中读取交易所股票的主列表。 第 2 阶段 - 从雅虎财经下载信息和股息,并运行一系列测试,以确定那些现在符合标准并被归类为目标的人,或者如果其中一个标准发生变化,可能在不久的将来/遥远的将来。 第 3 阶段 - 主列表和目标保存在电子表格中,并绘制目标以提供股息趋势和年度增长的视觉线索。

该程序的先前版本是用 3 个不同的脚本完成的,雅虎财经被要求下载每个阶段的股票信息。这些工作得很好,但我试图将所有这些整合到 1 个脚本中。

如下面提供的完整程序代码所示,我正在浏览我的股票列表并将信息保存在字典中:

graph_dict = {'Name':[],'Symbol':[],'Avg Div Growth':[],'Dividends':[],'Annual Dividends':[]}

名称和符号是字符串,Avg Div Growth 是浮点数,股息/年度股息是熊猫数据帧

当名字列表被循环播放时,雅虎财经获得了股息,字典被附加了。实际上,字典中的每个“行”都包含我绘制股票图表所需的所有信息。

我在填充字典时没有问题,但是当我尝试访问字典时,我有 3 个问题。

  1. 如何从字典中提取“行”?我可以使用以下命令从特定的“行”访问单个值:

graph_dict['Name'][row_num]

因此,graph_dict['Name'][0] 在下面的代码中是'吉宝房地产投资信托基金。

我无法弄清楚如何扩展行中的所有项目。我正在寻找等价物:

graph_dict.items()[row_num]

这将给我名称、符号、平均 div 增长、所有股息和股票的年度股息,以 gthat 行号。

  1. 如何按键 [Avg Div Growth] 中的值对“行”进行排序?

我想按照从最高平均股息增长到最低股息增长的顺序绘制证券。这就是我对名单上的证券进行排名的方式。

使用 sorted() :sorted_dict = sorted(graph_dict, key = lambda item: item['Avg Div Growth']))

和 itemgetter() :sorted_dict = sorted(graph_dict, key = itemgetter('Avg Div Growth')))

不适用于我的字典格式。

根据我如何声明字典,如何对字典中的“行”进行排序? 或者我需要以不同的方式声明我的词典吗?

  1. 如何对字典进行切片,以便我可以将一组“行”发送到我的图形函数?

我正在绘制证券列表,每页都有若干证券。因此,我需要对我的字典进行分组或切片,以便我可以将证券组及其股息数据发送到图形函数,并一次绘制一页(页面集合保存为.pdf文档以备将来参考)

我看到的一个答案建议如下:

group_to_graph = dict(list(sorted_graph_dict.items())[group_start:group_end])

其中 group start 和 end 是切片的限制,并引用字典中的“行”号。

这样做只是给了我group_start键的值,如下所示:

Stock Group on page # 0  - start row (group_start) =  0 , (end row (group_end) = 1 
 {'Name': ['Keppel REIT', 'UMS', 'SPH REIT', 'Frasers Hospitality Trust']}

Stock Group on page # 1  - start row (group_start) =  2 , (end row (group_end) = 3 
 {'Avg Div Growth': [6.77852254732552, 25.0433491073197, 32.833907784854, -20.4956238784202]}

我怀疑一旦我理解了如何访问字典中的完整“行”与上面的 Q1 中的单个键,这可能会自行回答,但请随时仔细阅读下面的代码并建议如何声明我的字典和访问“行”中的所有项目的选项。

import pandas as pd
import yfinance as yf

## basic information on each stock 
name = ['Keppel REIT','UMS','SPH REIT','Frasers Hospitality Trust']
symbol = ['K71U.SI','558.SI','SK6U.SI','ACV.SI']
avg_div_growth = [6.77852254732552,25.0433491073197,32.833907784854,-20.4956238784202]

## create dataframe to hold stock info (downloaded from yahoo finance)
df = pd.DataFrame (columns = ['Name','Symbol','Avg Div Growth (%)'])
df = df.astype( dtype = {'Name': str, 'Symbol': str, 'Avg Div Growth (%)': float})

df['Name'] = name
df['Symbol'] = symbol
df['Avg Div Growth (%)'] = avg_div_growth

## create dictionary to hold all relevant info for graphing. Each stock has its own 'row' in the lists within the dictionary
graph_dict = {'Name':[],
              'Symbol':[],
              'Avg Div Growth':[],
              'Dividends':[],
              'Annual Dividends':[]}   

## download dividend info from Yahoo Finance using yfinance
start = 0
end = len(df)

for count in range (start, end):    # loops through list of securities contained in the dataframe
    
    stock = yf.Ticker(df.loc[count,'Symbol'])
    
    div = stock.dividends   # is type Series by default    
    div_df = pd.DataFrame(div)  # change to Dataframe
    
    ## make annual dividends dataframe
    annual_divs_df = div_df.resample('Y').sum()
    annual_divs_df.rename(columns = {'Dividends':'Annual Dividends'}, inplace = True)
    
    ## populate each 'row' of the dictionary with the relevant data
    graph_dict['Name'].append(df.loc[count,'Name'])
    graph_dict['Symbol'].append(df.loc[count,'Symbol'])
    graph_dict['Avg Div Growth'].append(df.loc[count,'Avg Div Growth (%)'])
    graph_dict['Dividends'].append(div_df)
    graph_dict['Annual Dividends'].append(annual_divs_df)
    
print ('\nNumber of Names in dictionary is:',len(graph_dict['Name']))   # testing

## loop through dictionary to print each 'row' for testing purposes
for num in range(len(graph_dict['Name'])):
    print('\nStock Number:',num+1)
    
    print('Name is:', graph_dict['Name'][num])
    print('Symbol is:',graph_dict['Symbol'][num])
    print('Avg Annual Div Growth is:',graph_dict['Avg Div Growth'][num],'%')
    print('\nAll Dividends declared:\n',graph_dict['Dividends'][num])
    print('\nAnnual Dividends:\n',graph_dict['Annual Dividends'][num])
    
## sort the dictionary by Avg Div Growth from highest to lowest

''' How to sort the dictionary lists such that they are in order from highest Avg Div Growth to lowest ??? '''

sorted_graph_dict = graph_dict  # change once how to sort is figuired out


## get first row in Dictionary

''' How to extract all items in a single row of the list within the complete dictionary ??? '''

## group the entries in the dictionary into groups (pages in graph) of set size
graph_rows = 2

number_of_securities = len(sorted_graph_dict['Name'])
page_pad = number_of_securities % graph_rows

if page_pad == 0:   # all pages have full number of securities
    number_of_pages = number_of_securities // graph_rows

else:   # last page needs to be padded out to send proper number of securities to graph function
    number_of_pages = number_of_securities // graph_rows + 1

print ('\n\npage_pad = ',page_pad, 'number of pages is: ',number_of_pages)  # testing


start_page = 0
end_page = number_of_pages

group_to_graph = {} # empty dictionary to hold group of stocks to send to graph function

for page_number in range(start_page, end_page):
    
    group_start = (page_number + 1) * graph_rows - graph_rows
    group_end = group_start + graph_rows -1  
    
    
    ''' how to slice dictionary so 'rows' within group start and end are isolated and sent to graphing_function??? '''
    
    group_to_graph = dict(list(sorted_graph_dict.items())[group_start:group_end])       # DOES NOT WORK AS INTENDED
    
    print  ('\nStock Group on page #',page_number + 1,' - start row (group_start) = ',group_start, ', (end row (group_end) =',group_end, '\n',group_to_graph)  
    
    ''' 
    Should print out items in each row,instead it it prints:
    Names (key = 0 which is same as group_start) on 1st time through loop, and
    Avg Div Growth on ( key = 2 which is same as group_start) on 2nd time through loop
    '''
python 排序 字典 切片

评论

0赞 deadshot 3/13/2022
顶级域名;你的问题是什么?
0赞 LennyB 3/13/2022
有 3 个问题(问题:1、2、3)。Q1 是如何从字典中提取“行”,而不是逐个键提取。Q2 是如何根据一个列表中的值对“行”进行排序,Q3 是如何按“行”对字典进行切片。当我说“行”时,我的意思是在数据帧的意义上,其中每个“行”都是列表中的一只股票,“列”名称是键,单元格内容是值。我不确定如何操作字典。

答: 暂无答案