首页 > 代码库 > Python对字典(directory)按key和value排序

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}

 

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