python字符串按字典顺序排序-Python 3按值排序字典列表,其中值以字符串开...

试图弄清楚如何按值对字典列表进行排序,其中值以“自定义地图”列表中的字符串开头.例如,这里是要排序的数据:

"buckets": [

{

"doc_count": 23,

"key": "Major League Stuff"

},

{

"doc_count": 23,

"key": "Football Stuff"

},

{

"doc_count": 23,

"key": "Football Stuff > Footballs"

},

{

"doc_count": 23,

"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"

},

{

"doc_count": 4,

"key": "Jewelry > Rings"

},

{

"doc_count": 2,

"key": "All Gifts"

},

{

"doc_count": 2,

"key": "Gifts for Her"

},

{

"doc_count": 2,

"key": "Gifts for Her > Jewelry"

},

{

"doc_count": 2,

"key": "Football Stuff > Footballs > Tykes"

},

{

"doc_count": 1,

"key": "Brand new items"

},

{

"doc_count": 1,

"key": "Jewelry > Rings and Bands"

}

{

"doc_count": 1,

"key": "Football Stuff > Footballs > High School"

},

{

"doc_count": 1,

"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要求我编辑解释为什么这篇文章与其他“按价值排序”字样不同.在发布此内容之前,我确实查看了很多这样的内容,并且没有涉及匹配字符串部分的内容.所以我相信这不是重复的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值