python日常學習

2022-09-13 20:15:18 字數 2786 閱讀 1791

1 python title() 方法返回"標題化"的字串,就是說所有單詞都是以大寫開始,其餘字母均為小寫(見 istitle())

str.title()

2 類的繼承

class car():

def __init__(self, make, model, year):

self.make = make

self.model = model

self.year = year

self.odometer_reading = 0

def get_descriptive_name(self):

long_name = str(self.year) + ' ' + self.make + ' ' + self.model

return long_name.title()

def read_odometer(self):

print('this car has ' + str(self.odometer_reading) + ' miles on it.')

class electriccar(car):

def __init__(self, make, model, year):

super().__init__(make, model, year)

self.battery_size = 70

def describe_battery(self):

print("this car has a " + str(self.battery_size) + "-kwh battery.")

3 pd.read_csv() 

4

meshgrid函式通常在資料的向量化上使用,但是使用的方法我暫時還不是很明確。而meshgrid的作用適用於生成網格型資料,可以接受兩個一維陣列生成兩個二維矩陣,對應兩個陣列中所有的(x,y)對。接下來通過簡單的shell互動來演示一下這個功能的使用,並做一下小結。

互動顯示:

in [65]: xnums =np.arange(4)

in [66]: ynums =np.arange(5)

in [67]: xnums

out[67]: array([0,1, 2, 3])

in [68]: ynums

out[68]: array([0,1, 2, 3, 4])

in [69]: data_list= np.meshgrid(xnums,ynums)

in [70]: data_list

out[70]:

[array([[0, 1, 2,3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3]]), array([[0, 0, 0, 0],

[1, 1, 1, 1],

[2, 2, 2, 2],

[3, 3, 3, 3],

[4, 4, 4, 4]])]

in [71]: x,y =data_list

in [72]: x.shape

out[72]: (5l, 4l)

in [73]: y.shape

out[73]: (5l, 4l)

in [74]: x

out[74]:

array([[0, 1, 2,3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3]])

in [75]: y

out[75]:

array([[0, 0, 0,0],

[1, 1, 1, 1],

[2, 2, 2, 2],

[3, 3, 3, 3],

[4, 4, 4, 4]])

由上面的互動可以看出,meshgrid的作用是根據傳入的兩個一維陣列引數生成兩個陣列元素的列表。如果第乙個引數是xarray,維度是xdimesion,第二個引數是yarray,維度是ydimesion。那麼生成的第乙個二維陣列是以xarray為行,ydimesion行的向量;而第二個二維陣列是以yarray的轉置為列,xdimesion列的向量。

import numpy as np

import matplotlib.pyplot as plt

def height(x,y):

return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)

x=np.linspace(-3,3,300)

y=np.linspace(-3,3,300)

x,y=np.meshgrid(x,y)

plt.contourf(x,y,height(x,y),10,alpha=0.75,cmap=plt.cm.hot)

#為等高線填充顏色 10表示按照高度分成10層 alpha表示透明度 cmap表示漸變標準

c=plt.contour(x,y,height(x,y),10,colors='black')

#使用contour繪製等高線

plt.clabel(c,inline=true,fontsize=10)

#在等高線處新增數字

plt.xticks(())

plt.yticks(())

#去掉座標軸刻度

plt.show()

#顯示

請使用手機"掃一掃"x

Python學習日常

python range 函式可建立乙個整數列表,一般用在 for 迴圈中。語法 range start,stop,step 引數說明 start 計數從 start 開始。預設是從 0 開始。例如range 6 等價於range 0,6 stop 計數到 stop 結束,但不包括 stop。例如 ...

日常學習python一

今天是我開始學習python的第一天,留下一些記錄在這裡。我這次學習的是python3.python是一種指令碼語言,需要用直譯器來編寫 在linux中,我喜歡用vim進行編輯。這裡就不詳細解釋vim,畢竟它是linux的基礎。編輯完畢之後,需要給這個檔案乙個許可權,才能執行。利用如下 即可 chm...

python日常學習(一)

strip函式原型 宣告 s為字串,rm為要刪除的字串行.只能刪除開頭或是結尾的字元或是字串。不能刪除中間的字元或是字串。s.strip rm 刪除s字串中開頭 結尾處,位於 rm刪除序列的字元 s.lstrip rm 刪除s字串中開頭處,位於 rm刪除序列的字元 s.rstrip rm 刪除s字串...