如何根据深度嵌套的键对嵌套字典列表进行排序?
How to sort a list of nested dictionaries based on a deeply nested key?
我有一个如下所示的列表:
ls = [
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
如何按位于 quote
和 USD
键中的每个 market_cap
值对给定列表进行排序?
我找到了一些解决方案,但它们只是指按一级字典键和值排序,但是我还没有找到任何在二级字典中按键和值排序的解决方案深的。在给定列表中。
您可以将 lambda 函数作为内置 sorted
函数的 key
参数传递:
ls = [
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
sorted_ls = sorted(ls, key=lambda obj: obj["quote"]["USD"]["market_cap"])
或者,如果您想就地编辑列表,您可以使用 .sort()
方法。
您可以定义自己的函数,returns 作为排序依据的键,然后像这样使用 sort
all_data = [
{
"max_supply": null,
"platform": null,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": null,
"platform": null,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
def get_sort_key(current_dict):
return current_dict['quote']['USD']['market_cap']
all_data.sort(key=get_sort_key)
print(f'Sorted dict: {all_data}')
我有一个如下所示的列表:
ls = [
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
如何按位于 quote
和 USD
键中的每个 market_cap
值对给定列表进行排序?
我找到了一些解决方案,但它们只是指按一级字典键和值排序,但是我还没有找到任何在二级字典中按键和值排序的解决方案深的。在给定列表中。
您可以将 lambda 函数作为内置 sorted
函数的 key
参数传递:
ls = [
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": None,
"platform": None,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
sorted_ls = sorted(ls, key=lambda obj: obj["quote"]["USD"]["market_cap"])
或者,如果您想就地编辑列表,您可以使用 .sort()
方法。
您可以定义自己的函数,returns 作为排序依据的键,然后像这样使用 sort
all_data = [
{
"max_supply": null,
"platform": null,
"quote": {
"USD": {
"fully_diluted_market_cap": 464800388135.72,
"last_updated": "2021-10-20T13:37:02.000Z",
"market_cap": 464800388135.7246,
"price": 3938.871641392752,
"volume_24h": 15411634916.820467,
"volume_change_24h": -3.4909
}
}
},
{
"max_supply": null,
"platform": null,
"quote": {
"USD": {
"fully_diluted_market_cap": 58764548678.52,
"last_updated": "2021-10-20T13:37:03.000Z",
"market_cap": 58764548678.52,
"price": 4784.77,
"volume_24h": 189992412916.647,
"volume_change_24h": 85.422
}
}
},
]
def get_sort_key(current_dict):
return current_dict['quote']['USD']['market_cap']
all_data.sort(key=get_sort_key)
print(f'Sorted dict: {all_data}')