Python篇:sorted()的key的使用

C = ((0,1),(1,7),(2,8),(3,2),(4,1),(5,7))
print("C;",C)
result = sorted(list(enumerate(C)), key=lambda x: (x[1][1]))
print("paixu:",result)
x =[]
y =[]
for item in result:
    x.append(item[0])
    y.append(item[1])
print("x:",x)
print("y:",y)
plt.plot(x,y,'r.')
plt.show()

运行结果:

E:\Users\MrChao\venv\Scripts\python.exe E:/DataPycharm/DataML/machineLearn/test_1.py
C; ((0, 1), (1, 7), (2, 8), (3, 2), (4, 1), (5, 7))
paixu: [(0, (0, 1)), (4, (4, 1)), (3, (3, 2)), (1, (1, 7)), (5, (5, 7)), (2, (2, 8))]
x: [0, 4, 3, 1, 5, 2]
y: [(0, 1), (4, 1), (3, 2), (1, 7), (5, 7), (2, 8)]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_41297324/article/details/83901299