'buckets': [ { 'doc_count': 23,'key': 'Major League Stuff' },{ 'doc_count': 23,'key': 'Football Stuff' },'key': 'Football Stuff > Footballs' },'key': 'Football Stuff > Footballs > Pro' },{ 'doc_count': 22,'key': 'Football Stuff > Footballs > College' },{ 'doc_count': 20,'key': 'Football Stuff > Football Stuff Collections > Neat Stuff' },{ 'doc_count': 19,'key': 'Football Stuff > Helmets' },{ 'doc_count': 4,'key': 'Jewelry' },'key': 'Jewelry > Rings' },{ 'doc_count': 2,'key': 'All Gifts' },'key': 'Gifts for Her' },'key': 'Gifts for Her > Jewelry' },'key': 'Football Stuff > Footballs > Tykes' },{ 'doc_count': 1,'key': 'Brand new items' },'key': 'Jewelry > Rings and Bands' } { 'doc_count': 1,'key': 'Football Stuff > Footballs > High School' },'key': 'Football Stuff > Pads' }]
我想根据这个列表对它进行排序:
sort_map = ['Football Stuff','Jewelry','Gifts for Her','Brand new items','Major League Stuff','All Gifts']
我有点想“startwith”可以工作,但我不确定如何
buckets = sorted(buckets,key=lambda x: sort_map.index(x['key'].startswith[?]))
任何帮助赞赏!
旁注 – SO要求我编辑解释为什么这篇文章与其他“按价值排序”字样不同.在发布此内容之前,我确实查看了很多这样的内容,并且没有涉及匹配字符串部分的内容.所以我相信这不是重复的.
解决方法 我会利用你可以根据“>”拆分并获取第一个字段的索引这一事实buckets = sorted(buckets,key=lambda x: sort_map.index(x['key'].split(" > ")[0]))
要提供第二个Alpha标准,您可以返回一个元组作为第二个项目的完整字符串,以便在相同索引的情况下按字母顺序排序:
buckets = sorted(buckets,key=lambda x: (sort_map.index(x['key'].split(" > ")[0]),x['key']))总结
以上是内存溢出为你收集整理的Python 3按值排序字典列表,其中值以字符串开头全部内容,希望文章能够帮你解决Python 3按值排序字典列表,其中值以字符串开头所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)