根据另一个列表的字符串值对元组列表进行排序

2024-04-27 22:11:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试根据另一个基于金融股票的列表对金融数据列表进行排序

我的两个列表是包含公司财务数据信息的元组列表prc_list是具有(company_ticker, price_dataframe)形式的元素的元组列表,fin_list是具有(company_ticker, financial_dataframe, value)形式的元素的元组列表。在这里,“价格”只是指开盘/收盘价,“财务”是指在进行基本面分析时通常会看到的信息(如ROE、ROA等)

我要做的是首先取fin_list,然后根据特定的季度和键按降序排序。例如,如果我想根据2013-Q3的ROE对所有公司进行排序,那么第一家公司将显示2013年第三季度ROE值最高的公司的相关信息

在完成这个操作之后,我想对prc_list进行排序,以便标记器的顺序与fin_list的顺序相匹配

我的尝试如下:

prc_list = sorted(prc_list, key=(lambda x: fin_list[x][0]))
# Error -> "List indices must be integers or slices, not tuples."

tic_list = [fin_list[i][0] for i in range(len(fin_list))]
prc_list = sorted(prc_list, key=tic_list)
# Same error.

我们该怎么解决这个问题呢


Tags: 信息元素dataframe列表排序公司金融company
1条回答
网友
1楼 · 发布于 2024-04-27 22:11:55

这个怎么样

fin_list = [("ticker 1" , "price 1", 10),
            ("ticker 2" , "price 2", 0),
            ("ticker 3" , "price 3", 20),
            ("ticker 4" , "price 4", 14)]

# sort this list
fin_list_sorted = sorted(fin_list, key=lambda x: x[2])
print(fin_list_sorted)

# if you want to sort it in decreasing order
fin_list_sorted = sorted(fin_list, key=lambda x: -x[2])
print(fin_list_sorted)

# if you only want the attributes company_ticker and price_dataframe
prc_list = list(map(lambda x: x[0:2], fin_list_sorted))
print(prc_list)

相关问题 更多 >