sorted(iterable, /, *, key=None, reverse=False)¶
iterable是可遍历序列,key为按照某种函数的方法进行排序规则,reverse是按照排序大小排序,默认False是从小到大(True是从大到小)
假如有这样的字典:
y= {977: 101, 1011: 180, 1029: 62, 1006: 230, 984: 148, 969: 62, 999: 172, 1041: 21, 1042: 19, 1021: 130, 995: 207, 1008: 181, 1010: 193, 994: 193, 990: 184, 1040: 32, 992: 179, 972: 70, 986: 154, 973: 84, 1013: 151, 1030: 68, 1019: 137, 1028: 80, 963: 43, 1003: 191, 998: 195, 1004: 187, 1022: 111, 974: 92, 1034: 55, 996: 212, 968: 51, 965: 52, 1002: 203, 1001: 190, 1009: 182, 1018: 135, 1015: 154, 1027: 84, 1005: 184, 1012: 184, 1024: 83, 997: 169, 981: 139, 1000: 175, 993: 191, 987: 165, 991: 174, 1020: 130, 1026: 90, 938: 2, 1023: 108, 985: 151, 982: 110, 975: 96, 1017: 131, 989: 165, 1031: 59, 959: 20, 957: 22, 958: 32, 971: 80, 980: 125, 967: 48, 970: 77, 1014: 160, 978: 109, 1033: 56, 988: 157, 1051: 7, 976: 90, 979: 118, 1016: 138, 954: 14, 966: 48, 1007: 195, 1039: 32, 1036: 43, 1035: 37, 952: 12, 1043: 16, 983: 114, 953: 12, 961: 35, 1032: 45, 960: 27, 1038: 42, 955: 13, 962: 31, 1037: 37, 1046: 14, 1058: 2, 1025: 89, 1055: 4, 1049: 17, 950: 6, 964: 35, 956: 16, 1045: 14, 1050: 11, 947: 5, 949: 4, 951: 6, 945: 5, 1064: 1, 1056: 2, 928: 1, 1047: 12, 937: 1, 941: 3, 934: 1, 1044: 16, 948: 7, 918: 1, 1059: 4, 940: 1, 1048: 14, 944: 6, 1052: 3, 939: 1, 1054: 2, 1053: 8, 1070: 1, 1061: 1, 922: 1, 1063: 1, 929: 1, 946: 2, 926: 1, 942: 2, 943: 1, 1065: 1, 1068: 1, 931: 1, 1072: 1}
按照key排序
y = sorted(y.items(), key=lambda item: item[0], reverse=False)
[(918, 1), (922, 1), (926, 1), (928, 1), (929, 1), (931, 1), (934, 1), (937, 1), (938, 2), (939, 1), (940, 1), (941, 3), (942, 2), (943, 1), (944, 6), (945, 5), (946, 2), (947, 5), (948, 7), (949, 4), (950, 6), (951, 6), (952, 12), (953, 12), (954, 14), (955, 13), (956, 16), (957, 22), (958, 32), (959, 20), (960, 27), (961, 35), (962, 31), (963, 43), (964, 35), (965, 52), (966, 48), (967, 48), (968, 51), (969, 62), (970, 77), (971, 80), (972, 70), (973, 84), (974, 92), (975, 96), (976, 90), (977, 101), (978, 109), (979, 118), (980, 125), (981, 139), (982, 110), (983, 114), (984, 148), (985, 151), (986, 154), (987, 165), (988, 157), (989, 165), (990, 184), (991, 174), (992, 179), (993, 191), (994, 193), (995, 207), (996, 212), (997, 169), (998, 195), (999, 172), (1000, 175), (1001, 190), (1002, 203), (1003, 191), (1004, 187), (1005, 184), (1006, 230), (1007, 195), (1008, 181), (1009, 182), (1010, 193), (1011, 180), (1012, 184), (1013, 151), (1014, 160), (1015, 154), (1016, 138), (1017, 131), (1018, 135), (1019, 137), (1020, 130), (1021, 130), (1022, 111), (1023, 108), (1024, 83), (1025, 89), (1026, 90), (1027, 84), (1028, 80), (1029, 62), (1030, 68), (1031, 59), (1032, 45), (1033, 56), (1034, 55), (1035, 37), (1036, 43), (1037, 37), (1038, 42), (1039, 32), (1040, 32), (1041, 21), (1042, 19), (1043, 16), (1044, 16), (1045, 14), (1046, 14), (1047, 12), (1048, 14), (1049, 17), (1050, 11), (1051, 7), (1052, 3), (1053, 8), (1054, 2), (1055, 4), (1056, 2), (1058, 2), (1059, 4), (1061, 1), (1063, 1), (1064, 1), (1065, 1), (1068, 1), (1070, 1), (1072, 1)]
按照value排序(只是把item里的0改成1)
y = sorted(y.items(), key=lambda item: item[1], reverse=False)
[(1071, 1), (937, 1), (938, 1), (1055, 1), (1070, 1), (1063, 1), (1060, 1), (1058, 1), (929, 1), (1085, 1), (1068, 1), (941, 1), (942, 2), (1062, 2), (943, 2), (1059, 2), (940, 2), (936, 2), (1064, 2), (1061, 3), (1054, 3), (946, 3), (1052, 5), (945, 5), (947, 5), (944, 6), (1057, 6), (1053, 7), (949, 8), (1051, 8), (950, 8), (1056, 9), (1050, 9), (951, 10), (1049, 11), (1048, 11), (952, 11), (1044, 11), (948, 12), (1042, 12), (1047, 13), (955, 13), (1043, 14), (1046, 14), (960, 14), (954, 16), (953, 17), (1045, 17), (956, 19), (959, 20), (957, 22), (1041, 25), (958, 25), (961, 27), (962, 30), (1038, 31), (963, 32), (1039, 33), (965, 34), (1040, 35), (1037, 37), (1036, 43), (1034, 45), (967, 46), (1035, 47), (964, 49), (1033, 49), (966, 49), (968, 54), (1032, 55), (969, 57), (1030, 57), (970, 62), (1029, 64), (971, 66), (1028, 67), (1031, 67), (973, 75), (974, 79), (972, 83), (1023, 88), (975, 89), (976, 90), (1027, 92), (1025, 93), (1026, 100), (1021, 101), (977, 107), (1022, 107), (979, 112), (1020, 116), (1024, 118), (978, 121), (1018, 133), (1019, 134), (983, 137), (984, 138), (980, 138), (981, 139), (982, 140), (1016, 143), (1013, 144), (1015, 147), (1014, 152), (1017, 153), (992, 153), (1012, 154), (1011, 157), (988, 162), (1007, 165), (985, 166), (1010, 170), (987, 172), (986, 175), (993, 178), (1003, 179), (989, 179), (1009, 184), (998, 184), (1001, 186), (997, 187), (995, 194), (991, 194), (999, 197), (1005, 198), (1008, 198), (1006, 203), (1002, 203), (1004, 203), (1000, 204), (990, 208), (994, 210), (996, 214)]