方法一
nba_div_1 = "大西洋組"
nba_div_2 = "中央組"
nba_div_3 = "東南組"
nba_div_4 = "西北組"
nba_div_5 = "太平洋組"
nba_div_6 = "西南組"
nba_div_6 = "西南組"
方法二
nba_divs = ["大西洋組", "中央組", "東南組", "西北組", "太平洋組", "西南組"]
print(nba_divs[1])
list
# as of 2016-12-13
team_name = "金州勇士隊"
wins = 21
losses = 4
win_percentage = 0.84
is_on_winning_streak = True
golden_state_warriors = [team_name, wins, losses, win_percentage, is_on_winning_streak]
print(type(golden_state_warriors))
print(type(golden_state_warriors[0]))
tuple
nba_divs_tuple 不能做新增(.insert)或修改
nba_divs_tuple 不能做新增(.insert)或修改
dic
# as of 2016-12-13
team_name = "金州勇士隊"
wins = 21
losses = 4
win_percentage = 0.84
is_on_winning_streak = True
golden_state_warriors = { "team_name": team_name, "wins": wins, "losses": losses, "win_percentage": win_percentage, "is_on_winning_streak": is_on_winning_streak }
type(golden_state_warriors["team_name"])
type(golden_state_warriors["team_name"])
1. Python 的 list 無法使用 element-wise 運算
import numpy # 引入 numpy 套件
km_list = [21, 42.195]
km_array = numpy.array(km_list)
print(km_array)
print(type(km_array))
km_to_mile = 0.621371192
mile_array = km_array * km_to_mile
print(mile_array)
2. ndarray 只能容許一個資料類型
同時儲存有數值,布林值,會被自動轉換為數值同時儲存有數值,布林值與文字,會被自動轉換為文字
import numpy as np
my_list = [1, True]
#my_list = [1, True, "False"]
my_np_array = np.array(my_list)
print(type(my_list[1]))
print(type(my_np_array[1]))
print(my_np_array.dtype)
3. ndarray 可以建立矩陣
my_np_array = np.array([1, 2, 3, 4])
print(my_np_array)
print(my_np_array ** 2)
print("---") # 分隔一下
my_2d_array = np.array([[1, 3], [2, 4]])
print(my_2d_array)
print(my_2d_array ** 2)
4. 選擇 ndarray 的元素
用索引值選擇
用索引值選擇
import numpy as np
my_np_array = np.array([1, 2, 3, 4])
print(my_np_array[0])
my_2d_array = np.array([[1, 3],
[2, 4]])
print(my_2d_array[1, 1])
用布林值選擇
import numpy as np
my_np_array = np.array([1, 2, 3, 4])
filter = my_np_array >= 3
filter
out: array([False, False, True, True], dtype=bool)
import numpy as np
my_np_array = np.array([1, 2, 3, 4])
filter = my_np_array >= 3
filter
out: array([False, False, True, True], dtype=bool)
5. 了解外觀的屬性
.size.shape
my_np_array = np.array([1, 2, 3, 4])
print(my_np_array.size)
print(my_np_array.shape)
print("---") # 分隔一下 my_2d_array = np.array([[1, 3], [2, 4]])
print(my_2d_array.size)
print(my_2d_array.shape)
沒有留言:
張貼留言