import numpy as np
import matplotlib.pyplot as plt
import time
import math
plt.ion()
z = np.arange(-10,10,2)#自动生成列表,[-10,10)步长为2;
print(sum(z))
print(z)
'''''
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x,y1,'green')
time.sleep(10)
plt.plot(x,y2,'red')
plt.xlabel("x轴标签")
plt.ylabel("y轴标签")
plt.title("正弦图")
plt.plot(x,y1,linewidth=4) # 设置线的宽度
'''
'''''
cnt = 8
while cnt:
y = np.sin(x)
cnt -= 1
plt.pause(1)
if cnt&1 :
plt.plot(x, y, '-g')
else :
plt.plot(x, y, '-r')
plt.show()
plt.show()
'''
'''''
KNN算法 --->K-近邻
'''
import math
'''''
movie_data = {"宝贝当家": [45, 2, 9, "喜剧片"],
"美人鱼": [21, 17, 5, "喜剧片"],
"澳门风云3": [54, 9, 11, "喜剧片"],
"功夫熊猫3": [39, 0, 31, "喜剧片"],
"谍影重重": [5, 2, 57, "动作片"],
#"叶问3": [3, 2, 65, "动作片"],
"伦敦陷落": [2, 3, 55, "动作片"],
"我的特工爷爷": [6, 4, 21, "动作片"],
"奔爱": [7, 46, 4, "爱情片"],
"夜孔雀": [9, 39, 8, "爱情片"],
"代理情人": [9, 38, 2, "爱情片"],
"新步步惊心": [8, 34, 17, "爱情片"]}
x = [23, 3, 17]
KNN = []
for key, v in movie_data.items():
d = math.sqrt((x[0] - v[0]) ** 2 + (x[1] - v[1]) ** 2 + (x[2] - v[2]) ** 2)
KNN.append([key, round(d, 4)])
KNN= sorted(KNN,key = lambda zz:zz[1],reverse=True)
ZZZ = KNN[:3]
'''
labels = {"喜剧片":0,"动作片":0,"爱情片":0}
print(labels)
'''
for s in KNN:
label = movie_data[s[0]]
labels[label[3]] += 1
'''
'''
labels =sorted(labels.items(),key=lambda l: l[1],reverse=True)
labels =sorted(labels,key=lambda l: l[1],reverse=True)
两行的区别:第一行排序针对于第二个关键字进行排序且将原来labels的所有数据进行保存,并将其进行返回;
第二行排序和第一行一样,不过不保留关键字;
'''
#print(labels,labels[0][0],sep='\n')
kk1 =sorted(labels.items(),key=lambda l: l[1],reverse=True)
kk =sorted(labels,key=lambda l: l[1],reverse=True)
print(kk1)
print(kk)