Python对字典(directory)按key和value排序

distance = {9149: 0, 9150: 26, 9151: 24, 9152: 24, 9153: 24, 9154: 27, 9155: 25, 9156: 30, 9158: 20, 9159: 22, 9160: 25, 9161: 21, 9163: 25, 9164: 23, 9165: 25, 9166: 30, 9167: 20, 9169: 25, 9170: 24, 9171: 23, 9172: 25, 9173: 24, 9174: 29, 9176: 27, 9177: 25, 9179: 25, 9180: 32, 9181: 25, 9182: 25, 9183: 24, 9184: 25, 9185: 25, 9186: 25, 9188: 26, 9189: 20, 9190: 25, 9191: 27, 9193: 24, 9195: 24, 9196: 24, 9197: 23, 9198: 25, 9199: 25, 9201: 24, 9202: 25, 9203: 24, 9204: 25, 9205: 25, 9206: 25, 9208: 24, 9209: 22, 9210: 23, 9211: 22, 9212: 23, 9215: 25, 9217: 28, 9218: 25, 9219: 25, 9220: 33, 9221: 21, 9222: 25, 9224: 25, 9225: 25, 9226: 25, 9227: 24, 9228: 28, 9230: 22, 9231: 25, 9232: 24, 9233: 25, 9235: 24, 9236: 21, 9237: 22, 9238: 24, 9239: 22, 9240: 20, 9241: 24, 9242: 23, 9245: 22, 9248: 23, 9249: 22, 9250: 22, 9251: 23, 9252: 22, 9253: 22, 9254: 19, 9255: 23, 9256: 21, 9257: 23, 9258: 25, 9259: 23, 9260: 24, 9261: 22, 9263: 22, 9264: 23, 9265: 22, 9266: 22, 9267: 23, 9268: 25, 9269: 22, 9270: 22, 9271: 21, 9274: 24, 9276: 24, 9277: 25, 9279: 28, 9282: 21, 9283: 21, 9285: 22, 9286: 24, 9287: 24, 9289: 24, 9290: 25, 9292: 22, 9293: 22, 9296: 23, 9298: 21, 9299: 23, 9300: 22, 9302: 22, 9303: 25, 9304: 24, 9306: 25, 9307: 25, 9308: 22, 9311: 23, 9312: 25, 9315: 22, 9316: 24, 9317: 23, 9318: 22, 9320: 21, 9321: 28, 9324: 22, 9325: 21, 9328: 22, 9329: 22, 9330: 21, 9331: 25, 9332: 21}
print(distance)
#按value排序
print(sorted(distance.items(),key=lambda  item:item[1]))

#反序
#print(sorted(distance.items(),key=lambda  item:item[1],reverse=True))

#按Key排序
#print(sorted(distance.items(),key=lambda  item:item[0]))

#[(9149, 0), (9254, 19), (9158, 20), (9167, 20), (9189, 20), (9240, 20), (9161, 21), (9221, 21), (9236, 21), (9256, 21), (9271, 21), (9282, 21), (9283, 21), (9298, 21), (9320, 21), (9325, 21), (9330, 21), (9332, 21), (9159, 22), (9209, 22), (9211, 22), (9230, 22), (9237, 22), (9239, 22), (9245, 22), (9249, 22), (9250, 22), (9252, 22), (9253, 22), (9261, 22), (9263, 22), (9265, 22), (9266, 22), (9269, 22), (9270, 22), (9285, 22), (9292, 22), (9293, 22), (9300, 22), (9302, 22), (9308, 22), (9315, 22), (9318, 22), (9324, 22), (9328, 22), (9329, 22), (9164, 23), (9171, 23), (9197, 23), (9210, 23), (9212, 23), (9242, 23), (9248, 23), (9251, 23), (9255, 23), (9257, 23), (9259, 23), (9264, 23), (9267, 23), (9296, 23), (9299, 23), (9311, 23), (9317, 23), (9151, 24), (9152, 24), (9153, 24), (9170, 24), (9173, 24), (9183, 24), (9193, 24), (9195, 24), (9196, 24), (9201, 24), (9203, 24), (9208, 24), (9227, 24), (9232, 24), (9235, 24), (9238, 24), (9241, 24), (9260, 24), (9274, 24), (9276, 24), (9286, 24), (9287, 24), (9289, 24), (9304, 24), (9316, 24), (9155, 25), (9160, 25), (9163, 25), (9165, 25), (9169, 25), (9172, 25), (9177, 25), (9179, 25), (9181, 25), (9182, 25), (9184, 25), (9185, 25), (9186, 25), (9190, 25), (9198, 25), (9199, 25), (9202, 25), (9204, 25), (9205, 25), (9206, 25), (9215, 25), (9218, 25), (9219, 25), (9222, 25), (9224, 25), (9225, 25), (9226, 25), (9231, 25), (9233, 25), (9258, 25), (9268, 25), (9277, 25), (9290, 25), (9303, 25), (9306, 25), (9307, 25), (9312, 25), (9331, 25), (9150, 26), (9188, 26), (9154, 27), (9176, 27), (9191, 27), (9217, 28), (9228, 28), (9279, 28), (9321, 28), (9174, 29), (9156, 30), (9166, 30), (9180, 32), (9220, 33)]
#{9149: 0, 9150: 26, 9151: 24, 9152: 24, 9153: 24, 9154: 27, 9155: 25, 9156: 30, 9158: 20, 9159: 22, 9160: 25, 9161: 21, 9163: 25, 9164: 23, 9165: 25, 9166: 30, 9167: 20, 9169: 25, 9170: 24, 9171: 23, 9172: 25, 9173: 24, 9174: 29, 9176: 27, 9177: 25, 9179: 25, 9180: 32, 9181: 25, 9182: 25, 9183: 24, 9184: 25, 9185: 25, 9186: 25, 9188: 26, 9189: 20, 9190: 25, 9191: 27, 9193: 24, 9195: 24, 9196: 24, 9197: 23, 9198: 25, 9199: 25, 9201: 24, 9202: 25, 9203: 24, 9204: 25, 9205: 25, 9206: 25, 9208: 24, 9209: 22, 9210: 23, 9211: 22, 9212: 23, 9215: 25, 9217: 28, 9218: 25, 9219: 25, 9220: 33, 9221: 21, 9222: 25, 9224: 25, 9225: 25, 9226: 25, 9227: 24, 9228: 28, 9230: 22, 9231: 25, 9232: 24, 9233: 25, 9235: 24, 9236: 21, 9237: 22, 9238: 24, 9239: 22, 9240: 20, 9241: 24, 9242: 23, 9245: 22, 9248: 23, 9249: 22, 9250: 22, 9251: 23, 9252: 22, 9253: 22, 9254: 19, 9255: 23, 9256: 21, 9257: 23, 9258: 25, 9259: 23, 9260: 24, 9261: 22, 9263: 22, 9264: 23, 9265: 22, 9266: 22, 9267: 23, 9268: 25, 9269: 22, 9270: 22, 9271: 21, 9274: 24, 9276: 24, 9277: 25, 9279: 28, 9282: 21, 9283: 21, 9285: 22, 9286: 24, 9287: 24, 9289: 24, 9290: 25, 9292: 22, 9293: 22, 9296: 23, 9298: 21, 9299: 23, 9300: 22, 9302: 22, 9303: 25, 9304: 24, 9306: 25, 9307: 25, 9308: 22, 9311: 23, 9312: 25, 9315: 22, 9316: 24, 9317: 23, 9318: 22, 9320: 21, 9321: 28, 9324: 22, 9325: 21, 9328: 22, 9329: 22, 9330: 21, 9331: 25, 9332: 21}
时间: 2024-10-12 12:18:44

Python对字典(directory)按key和value排序的相关文章

python 根据字典中的key,value进行排序

#coding=utf-8 import requests,json,collections,base64,datetime def sort(datas): data=json.dumps(datas)#dumps是将dict转化成str格式,loads是将str转化成dict格式. print u"data %s" % data data=json.loads(data) print type(data) msg=data['msg'] #获取msg中key,value print

iOS_字典数组 按key分组和排序

// // main.m // SortGroup // // Created by beyond on 14-10-26. // Copyright (c) 2014年 beyond.com All rights reserved. // 按GroupTag分组和 按Order字段排序 enabled表示不启用 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepoo

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:

python 字典操作提取key,value

python 字典操作提取key,value dictionaryName[key] = value 1.为字典增加一项  2.访问字典中的值  3.删除字典中的一项  4.遍历字典  5.字典遍历的key\value  6.字典的标准操作符  7.判断一个键是否在字典中  8.python中其他的一些字典方法   原文地址:https://www.cnblogs.com/changfeng1800/p/8284125.html

python 输出字典的key

给你一字典a,如a={1:1,2:2,3:3},输出字典a的key,以','链接,如'1,2,3'. b=a.keys() m=[] for i in b:     m.append(str(i)) print ','.join(m)

python中字典以key排序,以value排序。以及通过value找key的方式

1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示可以迭代的对象,例如可以是dict.items().dict.keys()等,key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序(从大到小),reverse=false则是顺序(从小到大),默认是reverse=false. 2.

python 数据类型 ----字典

字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values id_db = { 'jboss':{ 'mycis': '10.88.130.38', 'ec':'10.88.130.29', 'rds':'10.88.130.25' }, 'jetty':{ 'mycis':'10.88.130.37', 'ec':'

python之字典应用

#author:zhouyu#字典,在Python里面也是经常用到的,它的存储方式是key:value,# 它是无序的,不像我们的字典,字典是用大括号{}括起来的a = {    'name':'zhouyu',    'age':22,    'sex':'male',    'QQ':'1090139534'}#打印出字典print(a)#打印指定的key的valueprint(a['name'])#修改keya['name'] = 'zhouziqi'#增加key和valuea['cont

python元字典

Python元字典 字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型.列表是有序的对象结合,字典是无序的对象集合. 两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取. 字典用"{ }"标识.字典由索引(key)和它对应的值value组成. #!/usr/bin/python # -*- coding: UTF-8 -*- dict = {} dict['one'] = "This is one" dict[2]

python学习 &nbsp; &nbsp; 字典

Python学习   字典 1)字典的了解 1.    字典是python中唯一的映射类型(哈希表) 2.    字典对象时可变的,但是字典的键必须使用不可变对象,并且在一个字典中可以使用不同的类型的键值. 3.    keys()或者是values()返回列表中或者值列表 4.    items()返回包含键值对应的元祖 1)  定义字典的方法和使用方法 1.字典名={key:values,key1:values1} 2.取值时: 字典名[key](取出与key相对应的values) 3.可以