在编写函数之前,我希望确保没有预构建(优化)的解决方案(如sorted())可以:
从这样的字典里:
tags = {'pinoyako':{'likes': 119, 'comments': 11, 'count': 1}, 'dii':{'likes': 151, 'comments': 3, 'count': 1},'djiphantom3':{'likes': 127, 'comments': 6, 'count': 1}}
根据“likes”、“comments”或“count”排序键。如果基于“likes”,则输出应该是一个按顺序排列的列表:
output = [['dii',151],['djiphantom3',127],['pinoyako',119]]
使用
sorted()
函数中的generator expression和适当的键函数:你需要更多的条目来说明。检查此项:
输出:
然后你可以这样做:
输出:
相关问题 更多 >
编程相关推荐