列表我只是想通過FIRST_NAME這個名單在Python排序那種字典詞典的蟒
list = [ { "profile" : { "first_name" : "a", "last_name" : "b" } } ,
{ "profile" : { "first_name" : "c", "last_name" : "d" } } ,
{ "profile" : { "first_name" : "e", "last_name" : "f" } } ]
列表我只是想通過FIRST_NAME這個名單在Python排序那種字典詞典的蟒
list = [ { "profile" : { "first_name" : "a", "last_name" : "b" } } ,
{ "profile" : { "first_name" : "c", "last_name" : "d" } } ,
{ "profile" : { "first_name" : "e", "last_name" : "f" } } ]
排序列表本身,使用:
lst.sort(key=lambda x: x['profile']['first_name'])
爲了保持lst
未排序並返回排序列表使用:
sorted(lst, key=lambda x: x['profile']['first_name'])
我已經嘗試瞭解決方案,但它返回'None'。 –
@AtulMakwana - 許多人使用'.sort()'語法而不是'sorted()' – Jonathan
這應做到:
>>> sorted(lst, key=lambda x: x['profile']['first_name'])
最好不要使用'list'作爲變量名稱 –