Python — dictionary pip 與 numpy === ###### tags: `python` `winter_camp` `tutorial` [TOC] # Dictionary dictionary 是一種 python特有的資料格式 用"type()"來查詢的話 會顯示:dict ## key 與 value ![](https://i.imgur.com/hRsvO41.png) 可以把 dictionary 的 key 想作是 list 的 index 在 ==Key== 的下面索引的的值 就是 ==value== ## Dictionary 的宣告 - { } | type | 符號 | | -------- | -------- | | tuple | ( ) | | list | [ ] | | dictionary | { } | | set | { } | ```python= dict1 = {} print(type(dict1)) ######### data = { "name" : "Eason", #key 不可以是 list , 兩個索引之間以 "," 隔開 5:31, "age" : 21, "hobby" : ["basketball","videogames"], "another_dict" : {"key1":"value"} } ``` ## 添加修改與刪除 key ```python= data["name"] = "Eric" #添加與修改 del data["name"] #刪除key del dict1 #直接刪除整個dictionary ``` ## 當key不在dictionary內時 ```python= data.keys() data.has_key("name") #python2.X 查詢key在不在dict裡面 data. __contains__("name") #python3.X 查詢key在不在dict裡面 data.has_key("car") ##############直接設立預設值##################### print(data["friend"]) data.get("friend"," None ") data.setdefault("friend","TW ice") ``` ## Dictionary with Json https://drive.google.com/file/d/11AgYNS6zUCh1n1yoiF6PSTtjz_ylaNA3/view ```python= import json with open('homework.json','r') as f: data=json.load(f) ``` # pip ## 簡介 pip 是 安裝 python模組 程式庫會好用的方式 ``` sudo apt install python-pip #python2 sudo apt install python3-pip #python3 pip install numpy pip3 install numpy ``` ## 推薦的模組: * matplotlib * numpy * tkinter * openCV * scikit-learn ## import ```python= import numpy #引入名為numpy的模組 print(numpy.pi) import numpy as np #引入名為numpy的模組,並命名為np print(np.pi) from numpy import pi print(pi) ``` # Numpy ## 一個好用的數學運算模組 提供與matlab 相似的功能 常常與 ==matplotlib== 一同使用 ### for example ```python= import numpy as np coeff = [3.2,2,1] #方程式係數為 3.2x^2+2x+1 np.roots(coeff) ``` **不過其實numpy這個功能博大精深,今天僅大概介紹最基礎的資料儲存部份** ## numpy array 與 list 的不同: numpy array 與 list 是很相像的 然list 中的子元素可以是各式各樣的,這種方式當然有他的方便之處,但是這樣靈活的儲存方式卻是相當的消耗記憶體與cpu 而numpy array 則規定了只能有一種資料儲存方式,這種結構與C++類似,效率也比較高 ## 宣告方式 ==注意 一定是用中括弧"[ ]包起來"== ### 直接列出來宣告 ```python= import numpy as np wrong = np.array(1,2,3) a=np.array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) #整數宣告 b=np.array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]],dtype=float) #浮點數宣告 #可用 np.dtype 查詢 print(b.dtype) ``` ### 特殊宣告功能 1. arrange ```python= array1 = np.arange(10) array2 = np.arange(1,10,0.1) #vs range list1 = list(range(10)) list2 = list(range(1,10,0.1)) #只能是整數 ``` 2. linspace ```python= array1 = np.linspace(0,8,100) #在 0~ 8 之間插入100個數 ``` 3. reshape ```python= array1 = np.linspace(0,8,100) array1 = array1.reshape(20,5) #將 array 變成 20*5的矩陣 array1 = np.linspace(0,7,100).reshape(4,25) #可以寫成一串 ``` 4. zeros & ones ```python= array1 = np.zeros((3,4)) array2 = np.ones((2,3,4)) #vs range是整數 ``` ## 基本計算 ### 加減乘除 此處的加減乘除並非矩陣的乘除,而是對相對應的數字做計算 a=10 20 30 40 50 b=1 2 3 4 5 ```python= a = np.arange(10,60,10) b = np.arange(1,6,1) print(a-b) print(a+b) print(a*b) print(a/b) print(b**2) a+=a #就像C++一樣 print(a) c = np.linspace(0,90,100) print(np.sin(np.deg2rad(c))) d = np.linspace(0,np.pi/2,100) print(np.sin(d)) print(c<45) ``` ### 矩陣相乘 A: | 1 | 1 | | -------- | -------- | | 0 | 1 | B: | 2 | 0 | | -------- | -------- | | 3 | 4 | ```python= A = np.array( [[1,1],[0,1]] ) B = np.array( [[2,0],[3,4]] ) print(A@B) print(A.dot(B)) ``` ## index array1 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | | -------- | -------- |-------- |-------- |-------- |-------- |-------- |-------- |-------- |-------- |-------- | array2 |10. | 10.90909091| 11.81818182| 12.72727273| 13.63636364| | -------- | -------- |-------- |-------- |-------- | | 14.5454545| 15.45454545| 16.3636363| 17.27272727| 18.18181818| | 19.0909090| 20. | 20.90909091| 21.81818182| 22.72727273| | 23.63636364| 24.54545455| 25.45454545| 26.3636363| 27.27272727| | 28.18181818| 29.09090909| 30. | 30.90909091| 31.81818182| | 32.72727273| 33.63636364| 34.54545455| 35.45454545| 36.36363636| | 37.27272727| 38.18181818| 39.09090909| 40. | 40.90909091| | 41.81818182| 42.72727273| 43.63636364| 44.54545455| 45.45454545| | 46.36363636| 47.27272727| 48.18181818| 49.09090909| 50| | 50.90909091| 51.81818182| 52.72727273| 53.63636364| 54.54545455| | 55.45454545| 6.36363636| 57.27272727| 58.18181818| 59.09090909| | 60.| 60.90909091| 61.81818182| 62.72727273| 63.63636364| | 69.09090909| 70. | 70.90909091| 71.81818182| 72.72727273| | 73.63636364| 74.54545455| 75.45454545| 76.36363636| 77.27272727| | 78.18181818| 79.09090909| 80. | 80.90909091| 81.81818182| | 82.72727273| 3.63636364|84.54545455| 85.45454545| 86.36363636| |87.27272727|88.18181818|89.09090909|90. | 90.90909091| |91.8181818|92.72727273|93.63636364|94.54545455| 95.45454545| | 96.36363636|97.27272727|98.18181818| 99.09090909| 100| ```python= array1 = np.linspace(10,100,10) array1[0] array1[0:3] array1[:] array1[::-1] array2 = np.linspace(10,100,100) array2 = array2.reshape(20,5) array2[:,:] array2[:,1:3] array2[2:4,1] array2[0,-2] #倒數第二個 ``` # homework ## hw1 讀取以下json檔,計算出10秒後,物體的位移(m) https://drive.google.com/file/d/11AgYNS6zUCh1n1yoiF6PSTtjz_ylaNA3/view?usp=sharing 備註: 1. 必須使用'numpy' 2. 假設輸入檔案名稱皆為homework.json 3. 若Json檔沒有此項資料,vel,force default=0 ,Mass default=1 hint : https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html Json資料的Key: ``` Mass : 質量(kg) vel_x: X方向初速(m/s) vel_y: Y方向初速(m/s) vel_z: Z方向初速(m/s) Force_x : X方向的分力(N) Force_y : Y方向的分力(N) Force_z : Z方向的分力(N) ``` #### Example: INPUT: ``` homework.json ``` OUTOUT: ``` position_x: 10 position_y: 50 position_z: 5 ``` ## hw2 用 matplotlib 描繪 sampling time 為 0.1秒的 x-t , y-t , z-t 圖