栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

按第一项对嵌套列表进行排序-itemgetter不能胜任

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

按第一项对嵌套列表进行排序-itemgetter不能胜任

更改键以将字符串转换为int会有所帮助,这也是其他一些排序技巧。

from operator import itemgetterlist_to_sort=[('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1),     ('89377', 1), ('228055', 1), ('228054', 1), ('228057', 2), ('228056', 1), ('228051', 1), ('228050', 1),('228053', 1), ('203184', 6), ('228059', 1), ('228058', 1), ('89370', 2), ('89371', 3), ('89372', 2), ('89373', 1), ('89374', 1), ('89375', 1), ('89376', 1), ('89377', 1)]print list_to_sortlist_to_sort.sort()print list_to_sort # badly sorted as describedlist_to_sort.sort(key=itemgetter(0))print list_to_sort # badly sorted as described (same as above)list_to_sort.sort(key=lambda x: int(x[0]))print list_to_sort # sorted welllist_to_sort.sort(key=lambda x: int(x[0]), reverse=True)print list_to_sort # sorted well in reverse

关于构建要从字典排序的列表的旁注。

iteritems()
是执行以下操作的更好方法

dict_List = [(x, FID_GC_dict[x]) for x in FID_GC_dict.keys()]dict_List = [(k,v) for k,v in FID_GC_dict.iteritems()]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号