Python对字典分别按键(key)和值(value)进行排序
使用sorted函数进行排序
sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数;
其中iterable表示可以迭代的对象,例如可以是dict.items()、dict.keys()等
key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序,
reverse=false时则是顺序,默认时reverse=false。
初始化字典
dict_data={6:9,10:5,3:11,8:2,7:6}
1、对字典按键(key)进行排序
对字典按键(key)进行排序(默认由小到大)
test_data_0=sorted(dict_data.keys())
输出结果
print(test_data_0) #[3, 6, 7, 8, 10]
test_data_1=sorted(dict_data.items(),key=lambda x:x[0])
输出结果
print(test_data_1) # [(3, 11), (6, 9), (7, 6), (8, 2), (10, 5)]
2、对字典按值(value)进行排序
#对字典按值(value)进行排序(默认由小到大)
test_data_2=sorted(dict_data.items(),key=lambda x:x[1])
#输出结果
print(test_data_2) #[('8', 2), ('10', 5), ('7', 6), ('6', 9), ('3', 11)]
test_data_3=sorted(dict_data.items(),key=lambda x:x[1],reverse=True)
#输出结果
print(test_data_3) #[('3', 11), ('6', 9), ('7', 6), ('10', 5), ('8', 2)]
方法二:
import operator
#初始化字典
dict_data={6:9,10:5,3:11,8:2,7:6}
#按键(key)进行排序
test_data_4=sorted(dict_data.items(),key=operator.itemgetter(0))
test_data_5=sorted(dict_data.items(),key=operator.itemgetter(0),reverse=True)
print(test_data_4) #[(3, 11), (6, 9), (7, 6), (8, 2), (10, 5)]
print(test_data_5) #[(10, 5), (8, 2), (7, 6), (6, 9), (3, 11)]
#按值(value)进行排序
test_data_6=sorted(dict_data.items(),key=operator.itemgetter(1))
test_data_7=sorted(dict_data.items(),key=operator.itemgetter(1),reverse=True)
print(test_data_6) #[(8, 2), (10, 5), (7, 6), (6, 9), (3, 11)]
print(test_data_7) #[(3, 11), (6, 9), (7, 6), (10, 5), (8, 2)]
demo:
现有字典d={'a':24, 'b':32, "c":12}请分别按字典只中的key、value排序?
sorted(iterable, key, reverse) 三个参数,iterable是可迭代对象,key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是正序, 默认reverse=False.
按key排序(只是输出的key值的排序列表):
sorted(d.keys(), reverse=True/False)
按value排序(只是输出的value值的排序列表):
sorted(d.values(), reverse=True/False)
sorted函数返回值是list,上述两种也可以用于提取key或value的列表
按key:value中的key排序:
sorted(d.items(),key=lambda item:item[0], reverse=True/False) 输出[(key,value), (key,value)]的排序
按key:value中的value排序排序:
sorted(d.items(),key=lambda item:item[1], reverse=True/False) 输出[(key,value), (key,value)]的排序
Python对字典分别按键(key)和值(value)进行排序的更多相关文章
- python 对字典分别按照key值、value值进行排序
1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...
- 浅谈python中字典append 到list 后值的改变问题
看一个例子 ? 1 2 3 4 d={'test':1} d_test=d d_test['test']=2 print d 如果你在命令行实践的话,会发现你改动的是d_test ,但是d 也跟着改变 ...
- python 得到字典的所有键 和值
a={} a={"a":1,"b":2,"c":3,"d":4} print(a) print(a.items()) p ...
- python 根据字典中的key,value进行排序
#coding=utf-8 import requests,json,collections,base64,datetime def sort(datas): data=json.dumps(data ...
- python 遍历字典中的键和值
#遍历字典中的所有键和值 zd1={"姓名":"张三","年龄":20,"性别":"女"} zd2= ...
- python 合并字典,相同 key 的 value 如何相加?
x = { 'apple': 1, 'banana': 2 } y = { 'banana': 10, 'pear': 11 } 需要把两个字典合并,最后输出结果是: { 'apple': 1, 'b ...
- python中字典dic详解-创建,遍历和排序
原文地址:http://www.bugingcode.com/blog/python_dic_create_sort.html 在python的编程中,字典dic是最典型的数据结构,看看如下对字典的操 ...
- Java中Map根据键值(key)或者值(value)进行排序实现
我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...
- python中字典的建立
一.字典由键key与值value构成. 如: a={'d':6,'f':'va'}print(a['f']) 上面代码简单建立字典,其中需要访问字典需要输入键值. 二.又比如需要在某个关键字中添加数据 ...
随机推荐
- mac终端解决很多系统自带命令找不到问题
node安装提示npm command not found 1.打开终端 2.输入命令如下: touch~/.bash_profile (创建.bash_profile文件,-表示在-目录下,.表示 ...
- Layedit 编辑页面赋值
1.编辑页面 $("[name=Experience]").val(data.Experience);//直接赋值然后再进行build experience = layedit.b ...
- 004-linux下配置rsyslog日志收集服务器案例 rsyslog+loganalyzer日志服务器,无法添加报表模板解决
centos6系统 client1:192.168.1.33 centos7系统 client2:192.168.1.44 centos7系统 master:192.168.1.55 配置服务端mas ...
- HDU - 6395 Sequence (整除分块+矩阵快速幂)
定义数列: $\left\{\begin{eqnarray*} F_1 &=& A \\ F_2 &=& B \\ F_n &=& C\cdot{}F_ ...
- centos7中使用yum安装tomcat mysql 等
安装Tomcat 进入 # cd /usr/local/tomcat # wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7 ...
- 使用 mybatis-Generator 自动生成DAO、Model、Mapping相关文件
1.Maven项目 2.配置generatorConfig.xml文件 3.在pom.xml中配置插件 2.generatorConfig.xml文件信息 <?xml version=" ...
- 字符串格式化-String类format方法
常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象. format()方法有两种重载形式. 显示不同转换符实现不同数据类型到字符串的转换,如图所示: 测 ...
- 一个python生成器的使用
def myegg(retlist): for i in retlist: rec = yield i mydict = [ {}, {}, {}, ] b = myegg(mydict) obj = ...
- 编程语言类别;运行Python程序的方式;变量和常量;Python程序的垃圾回收机制;
目录 编程语言分类 运行Python程序的两种方式 1.交互式 变量与常量 1.变量 2.常量 3.小整数池 垃圾回收机制 编程语言分类 编程语言分为: 1.机器语言:直接用二进制的0和1和计算机(C ...
- ython CGI编程Ⅱ
HTTP头部 hello.py文件内容中的" Content-type:text/html"即为HTTP头部的https://www.xuanhe.net/一部分,它会发送给浏览器 ...