python基础代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础代码相关的知识,希望对你有一定的参考价值。
1 from heapq import *; 2 from collections import *; 3 import random as rd; 4 import operator as op; 5 import re; 6 7 data = [2,2,6,7,9,12,34,0,76,-12,45,79,102]; 8 s = set(); 9 10 for num in data: 11 s.add(data.pop(0)); 12 if s.__len__() == 4: 13 break; 14 15 heap = []; 16 for n in s: 17 heappush(heap,n); 18 19 print(heap); 20 21 22 for num in data: 23 if num > heap[0]: 24 heapreplace(heap,num); 25 26 print(nlargest(4,heap)) 27 28 29 def file2matrix(path,dimension): 30 with open(path,‘r+‘) as fr: 31 lines = fr.readlines(); 32 num_lines = len(lines); 33 return_mat = np.zeros((num_lines,dimension)); 34 classLabel = []; 35 36 index = 0; 37 for line in lines: 38 contents = line.strip().split(‘ ‘); 39 li = contents[:dimension]; 40 li = list(map(float,li)); 41 return_mat[index,:] = li; 42 43 if(contents[-1] == ‘small‘): 44 classLabel.append(0); 45 elif(contents[-1] == ‘middle‘): 46 classLabel.append(1) 47 elif (contents[-1] == ‘large‘): 48 classLabel.append(2) 49 index += 1; 50 51 return return_mat, classLabel; 52 53 #mat,label = file2matrix(‘G:\\test.txt‘,3); 54 55 import collections; 56 print(dir(collections)) 57 58 class MyObject: 59 def __init__(self,score): 60 self.score = score; 61 62 def __repr__(self): 63 return "MyObject(%s)" % self.score; 64 65 objs = [MyObject(i) for i in range(5)]; 66 rd.shuffle(objs); 67 print(objs); 68 69 g = op.attrgetter("score"); 70 scores = [g(i) for i in objs]; 71 print("scores: ",scores); 72 print(sorted(objs,key = g)); 73 74 l = [(i,i*-2) for i in range(4)] 75 print ("tuples: ", l) 76 g = op.itemgetter(1) 77 vals = [g(i) for i in l] 78 print ("values:", vals) 79 print ("sorted:", sorted(l, key=g)) 80 81 82 class MyObj(object): 83 def __init__(self, val): 84 super(MyObj, self).__init__() 85 self.val = val 86 return 87 88 def __str__(self): 89 return "MyObj(%s)" % self.val 90 91 def __lt__(self, other): 92 return self.val < other.val 93 94 def __add__(self, other): 95 return MyObj(self.val + other.val) 96 97 98 a = MyObj(1) 99 b = MyObj(2) 100 101 print(op.lt(a, b)) 102 103 print(op.add(a, b)) 104 105 106 107 items = [(‘A‘, 1),(‘B‘, 2),(‘C‘, 3)] 108 regular_dict = dict(items); 109 order_dict = OrderedDict(items); 110 print(regular_dict); 111 print(order_dict); 112 113 114 # -*- coding: utf-8 -*- 115 import numpy as np 116 import matplotlib.pyplot as plt 117 from matplotlib.lines import Line2D 118 119 x = np.linspace(0, 10, 1000) 120 y = np.sin(x) 121 z = np.cos(x**2) 122 123 fig = plt.figure(figsize=(8,4),dpi=120) 124 plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2) 125 plt.plot(x,z,"b--",label="$cos(x^2)$") 126 plt.xlabel("Time(s)") 127 plt.ylabel("Volt") 128 plt.title("PyPlot First Example") 129 plt.ylim(-1.2,1.2) 130 plt.legend() 131 #plt.show() 132 133 134 f = plt.gcf(); 135 all_lines = plt.getp(f.axes[0],‘lines‘); 136 print(all_lines[0]) 137 138 fig = plt.figure() 139 line1 = Line2D([0,1],[0,1], transform=fig.transFigure, figure=fig, color="r") 140 line2 = Line2D([0,1],[1,0], transform=fig.transFigure, figure=fig, color="g") 141 fig.lines.extend([line1, line2]) 142 fig.show() 143 144 def autonorm(dataSet): 145 minVals = dataSet.min(0); 146 maxVals = dataSet.max(0); 147 ranges = maxVals - minVals; 148 rows = dataSet.shape[0]; 149 ranges = np.tile(ranges,(rows,1)); 150 dataSet = dataSet - np.tile(minVals,(rows,1)); 151 normData = dataSet / ranges; 152 return normData; 153 154 155 def classify(inX,path,k): 156 #1.文件到矩阵的映射 157 labels,dataSet = file2matrix(path); 158 #2.矩阵归一化处理 159 dataSet = autonorm(dataSet); 160 #3.计算欧式距离 161 distance = dataSet - inX; 162 distance = np.square(distance); 163 distance = distance.sum(axis=1); 164 distance = np.sqrt(distance); 165 print(distance); 166 #4.对距离排序 167 sortdisIndices = distance.argsort(); 168 #5.取前k个,加载到dict中,然后对dict排序,取首个值 169 classCount = {}; 170 for index in range(k): 171 label = labels[sortdisIndices[index]]; 172 print(label) 173 classCount[label] = classCount.get(label,0) + 1; 174 175 sortedDict = sorted(classCount.items(),key=op.itemgetter(1),reverse=True); 176 return sortedDict[0][0]; 177 178 def file2matrix(filepath): 179 with open(filepath,‘r+‘) as fr: 180 lines = fr.readlines(); 181 num_lines = len(lines); 182 classLabelVector = []; 183 dimension = len(lines[0].strip().split(" "))-1; 184 dataSet = np.zeros((num_lines,dimension)); 185 186 index = 0; 187 for line in lines: 188 contents = line.strip().split(" "); 189 li = contents[:dimension]; 190 li = list(map(float,li)); 191 dataSet[index,:] = li; 192 193 if contents[-1] == ‘largeDoses‘: 194 classLabelVector.append(3); 195 elif contents[-1] == ‘smallDoses‘: 196 classLabelVector.append(2); 197 elif contents[-1] == ‘didntLike‘: 198 classLabelVector.append(1); 199 index += 1; 200 201 return classLabelVector,dataSet; 202 203 204 205 206 def main(): 207 208 inX = np.array([1.2,1.0,0.8]); 209 label = classify(inX,"E:\\Python\\datingTestSet.txt",3); 210 print("class:",label); 211 212 213 if __name__ == ‘__main__‘: 214 main();
以上是关于python基础代码的主要内容,如果未能解决你的问题,请参考以下文章