python 字典排序 字典序排序是什么意思
1个回答
展开全部
字典就是包含key:value对的集合
比如
L = [{"name": "zhangsan", "birth":19920208, "gender":"m"}, {"name": "lisi", "birth":19900609, "gender":"m"}, {"name": "wanghong", "birth":19950907, "gender":"f"}]
对字典排序就是对列表进行排序后打印:
from operator import itemgetter
L = sorted(L,key=itemgetter('birth'),reverse=True)
for i in range(0,len(L)):
... print("Name: {:10} Gender: {:2} Birth: {}".format(L[i]["name"],L[i]["gender"],L[i]["birth"]))
比如
L = [{"name": "zhangsan", "birth":19920208, "gender":"m"}, {"name": "lisi", "birth":19900609, "gender":"m"}, {"name": "wanghong", "birth":19950907, "gender":"f"}]
对字典排序就是对列表进行排序后打印:
from operator import itemgetter
L = sorted(L,key=itemgetter('birth'),reverse=True)
for i in range(0,len(L)):
... print("Name: {:10} Gender: {:2} Birth: {}".format(L[i]["name"],L[i]["gender"],L[i]["birth"]))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询