当前位置:网站首页>Counter统计数量后,如何返回有序的key
Counter统计数量后,如何返回有序的key
2022-07-03 02:39:00 【strawberry47】
x = Counter({'a':10, 'b':3, 'c':7})
返回的是counterCounter({'a': 10, 'c': 7, 'b': 3})
,我只想取出有序的key,该怎么做呢?
- 如果直接使用
x.keys()
返回的是dict_keys(['a', 'b', 'c'])
,并非我们想要的顺序 - 使用
x.most_common(3)
返回频率最高的3个keys[('a', 10), ('c', 7), ('b', 3)]
,是元组和列表形式 - 正确做法:
sorted(x, key=x.get, reverse=True)
会返回['a', 'c', 'b']
边栏推荐
- Xiaodi notes
- Gbase 8C system table PG_ class
- Gbase 8C system table PG_ constraint
- [fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
- SQL statement
- 错误Invalid bound statement (not found): com.ruoyi.stock.mapper.StockDetailMapper.xxxx解决
- A2L file parsing based on CAN bus (2)
- Kubernetes family container housekeeper pod online Q & A?
- 为什么会选择框架?选择什么样的框架
- GBase 8c触发器(三)
猜你喜欢
《MATLAB 神经网络43个案例分析》:第43章 神经网络高效编程技巧——基于MATLAB R2012b新版本特性的探讨
easyExcel
Kubernetes family container housekeeper pod online Q & A?
Choose it when you decide
Pytorch convolution network regularization dropblock
oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???
HW initial preparation
Awk from introduction to earth (0) overview of awk
HW-初始准备
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
随机推荐
Gbase 8C function / stored procedure parameters (II)
[Hcia]No.15 Vlan间通信
Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
Classes and objects - initialization and cleanup of objects - constructor call rules
Gbase 8C system table PG_ database
sql server 查询指定表的表结构
GBase 8c系统表-pg_am
awk从入门到入土(0)awk概述
Gbase 8C system table PG_ authid
Restcloud ETL cross database data aggregation operation
What does "where 1=1" mean
GBase 8c系统表-pg_class
Current situation and future of Web3 in various countries
GBase 8c系统表pg_cast
COM and cn
GBase 8c系统表-pg_constraint
easyPOI
【翻译】后台项目加入了CNCF孵化器
Javescript 0.1 + 0.2 = = 0.3 problem
Gbase 8C system table PG_ conversion