[英]extract data from dictionary with nested dictionaries that contain lists that contain dictionaries
[英]Python: Sorting lists of dictionaries that contain lists of other dictionaries based on length of a member in the nested dictionary
我的數據結構示例如下所示:
主要清單:
[{..}, {..}, ..] # list of dictionaries
主列表存儲列表元素字典:
{
'queues': [{..}, {..}, ..], # list of more dictionaries
}
內部列表元素 dict :
{
'member': ["string", "string", ..] # list of strings
}
我的目標是根據內部詞典中成員列表的長度對主列表進行排序。 什么是實現這一目標的有效和 Pythonic 方式?
您可以將sorted
或list.sort
與適當的鍵 function 一起使用。 如果按所有member
列表的總長度排序:
main_list.sort(key=lambda d: sum(len(x['member']) for x in d['queues']))
或者,如果您只想始終考慮第一個相關的:
main_list.sort(key=lambda d: len(d['queues'][0]['member']))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.