目錄
數據類型和結構
4.1.1 整數
a = 10
# 獲取對象類型
print(type(a)) # <class 'int'>
# 獲取int對象所需位數
print(a.bit_length()) # 4
b = 100000
print(b.bit_length()) # 17
googol = 10 ** 100
print(googol)
#10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(googol.bit_length()) # 333
c = 1 + 4
print(c) # 5
d = 1 / 4
print(d) #0.25
print(type(d)) #<class 'float'>
4.1.2 浮點數
print(1. / 4) # 0.25
print(type(1. / 4)) # <class 'float'>
# 浮點對象
b = 0.35
print(type(b)) # <class 'float'>
print(b + 0.1) # 0.44999999999999996
# 進行精確存儲
c = 0.5
print(c.as_integer_ratio()) # (1, 2)
# 精度取決於表示數值所用的位數 ,一般來說, Python 運行的所有平臺使用lEEE 754精度標準( 64 位)作爲內部表示
# 這相當於 15 位數字的相對精度
print(b.as_integer_ratio()) # (3152519739159347, 9007199254740992)
# decimal模塊,爲浮點數提供了任意精確度的對象
import decimal
from decimal import Decimal
print(decimal.getcontext()) # Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
d = Decimal(1) / Decimal(11)
print(d) # 0.09090909090909090909090909091
print(1 / 11) # 0.09090909090909091
#decimal 模塊提供一個任意精度浮點數對象.在金融中,確保高精度、超出 64 位雙精度標準有時是必要的.
# 可以改變 Context 對象的各個屬性值,從而改變表示精度:
decimal.getcontext().prec = 4 # 低精度
e = Decimal(1) / Decimal(11)
print(e) # 0.09091
decimal.getcontext().prec = 50 # 高精度
f = Decimal(1) / Decimal(11)
print(f) # 0.090909090909090909090909090909090909090909090909091
g = d + e + f
print(g) #0.27272818181818181818181818181909090909090909090909
4.1.3 字符串
t = 'this is a string object'
# 將第一個詞的首字母轉爲大寫字母
print(t.capitalize()) # This is a string object
# 將字符串拆分成單個詞,獲得包含所有單詞的列表對象
print(t.split()) # ['this', 'is', 'a', 'string', 'object']
# 搜索一個單詞,如果成功,可以得到該詞第一個字母的位置(索引index)
print(t.find('string')) # 10
# 如果該詞不在字符串對象中,則返回-1
print(t.find('xu')) # -1
# 替換字符串對象中內容,使用replace
t.replace(' ', '|')
print(t.replace(' ', '|')) # this|is|a|string|object
# 剝離Stripping操作,刪除某些前導或者後綴字符
s = 'http://www.python.org'.strip('htp:/')
print(s) # www.python.org
# 正則表達式
import re
# 假定你面對一個大的文本文件,例如逗號分隔值 (CSV) 文件,其中包含了某些事件
# 序列和相應的時期-時間信息 日期-時間信息多中以 Python 無法直接解釋的格式提供
# 然而,日期-時間信息、通常可以通過正則表達式描述 考慮如下的字符串對象,它包含
# 3個日期.時間元素、 3個整數和3 個字符串。 注意, 3個引號可以在多行上定義字符串:
series = """
'01/18/2020 13:00:00',100,'1st';
'01/18/2020 13:30:00',110,'2nb';
'01/18/2020 14:00:00',120,'3rd';
"""
# 下面,正則表達式要對上述字符串對象中提供的日期-時間信息格式進行描述
dt = re.compile("'[0-9/:\s]+'")
result = dt.findall(series)
print(result) # ["'01/18/2020 13:00:00'", "'01/18/2020 13:30:00'", "'01/18/2020 14:00:00'"]
from datetime import datetime
pydt = datetime.strptime(result[0].replace("'", ""),
'%m/%d/%Y %H:%M:%S')
print(pydt) # 2020-01-18 13:00:00
print(type(pydt)) #<class 'datetime.datetime'>
4.1.4元祖
# 高級的數據結構,通過圓括號提供對象定義
t = (1, 2.5, 'data')
print(type(t)) # <class 'tuple'>
# 也可以取消掉括號,用逗號分隔開
t = 1, 2.5, 'data'
print(type(t)) # <class 'tuple'>
print(t[2]) # data
print(type(t[2])) # <class 'str'>
# 某個對象出現的次數
print(t.count('data')) # 1
# 某個對象第一次出現的索引值
print(t.index(1)) # 0
# 元祖對象不是很靈活,一旦定義,不容易更改
4.1.5 列表
# 列表類型的對象更靈活、強大,很多工作只能用列表對象完成,例如存儲股票報價、附加新數據。
# 列表通過方括號定義
l = [1, 2.5, 'data']
print(l[2]) # data
t = (1, 2.5, 'data')
l = list(t)
print(l) # [1, 2.5, 'data']
print(type(l)) # <class 'list'>
# 除了元組對象的特性之外,列表對象還可以通過不同方法擴展和縮小
# 換句話說,字符串和元組對象是不可變序列對象(具有索引),一經創建不能更改,
# 而列表對象是可變的,可以通過不同操作更改 可以在現有列表對象中附加一個或者多個列表對象:
l.append([4, 3]) # 在列表中追加列表子項
print(l) # [1, 2.5, 'data', [4, 3]]
l.append([1.0, 1.5, 2.0])
print(l) # [1, 2.5, 'data', [4, 3], [1.0, 1.5, 2.0]]
#insert在index位置插入
l.insert(1, 'insert')#在index位置爲1處插入insert
print(l) #[1, 'insert', 2.5, 'data', [4, 3], [1.0, 1.5, 2.0]]
#remove刪除列表元素
l.remove('data')
print(l) #[1, 'insert', 2.5, [4, 3], [1.0, 1.5, 2.0]]
#pop 刪除並返回刪除列表
p = l.pop(3)
print(l) #[1, 'insert', 2.5, [1.0, 1.5, 2.0]]
print(p) #[4, 3]
#選擇某段列表元素l[m:n],不包括n
print(l[1:3]) #['insert', 2.5]
4.1.6 控制結構
l = [2, 2.5, 2.4, 1 / 3, 0.8, 13]
for element in l[2:5]:
print(element ** 2)
# 5.76
# 0.1111111111111111
# 0.6400000000000001
# 通過range方式與上述一致
r = range(0, 8, 1) # 開始,結束,步長
print(r) # range(0, 8)
for i in range(2, 5):
print(l[i] ** 2)
# 列表推導,很有用
m = [i ** 2 for i in range(5)]
print(m) # [0, 1, 4, 9, 16]
4.1.7 函數式編程
def f(x):
return x ** 2
def even(x):
return x % 2 == 0
print(f(2)) # 4
print(even(3)) # False
# 利用map應用到整個列表的對象
map(even, range(10)) # [True,False,....]
map(lambda x: x ** 2, range(10)) # [0,1,4,9,...,81]
filter(even, range(15)) # [0,2,...14]
4.1.8 字典
# 字典是無序、不可排序的。
# 列表對象是有序可排序的
d = {
'Name': "Andy",
'County': "China",
'Profession': 'Chancelor',
'Age': 60
}
print(type(d)) # <class 'dict'>
print(d['Name'], d['Age']) # Andy 60
print(d.keys()) # dict_keys(['Name', 'County', 'Profession', 'Age'])
print(d.values()) # dict_values(['Andy', 'China', 'Chancelor', 60])
print(d.items()) # dict_items([('Name', 'Andy'), ('County', 'China'), ('Profession', 'Chancelor'), ('Age', 60)])
print(d['Age'] + 1)
# 字典對象中獲得迭代器對象
for item in d.items():
print(item)
'''
('Name', 'Andy')
('County', 'China')
('Profession', 'Chancelor')
('Age', 60)
'''
for value in d.values():
print(type(value))
'''
<class 'str'>
<class 'str'>
<class 'str'>
<class 'int'>
'''
4.1.9 集合
'''
集合set對象,沒有太多實用價值,因爲無序,每個元素只包含一次
'''
s = set(['u', 'd', 'ud', 'du', 'd', 'du'])
print(s) # {'ud', 'u', 'd', 'du'}
print(type(s)) # <class 'set'>
t = set(['d', 'dd', 'uu', 'u'])
# 實現並交差集
# 並
print(s.union(t)) # {'du', 'uu', 'd', 'ud', 'dd', 'u'}
# 交
print(s.intersection(t)) # {'d', 'u'}
# 差
print(s.difference(t)) # {'ud', 'du'}
# 在其中一箇中,但不是每個都在
print(s.symmetric_difference(t)) # {'uu', 'ud', 'du', 'dd'}
'''
集合的應用:去掉列表對象中的重複項
'''
import random
# 在0-10中間選取1000個數
l = [random.randint(0, 10) for i in range(1000)]
print(len(l))
print(l[:20])
s = set(l)
print(s)
'''
1000
[8, 3, 10, 10, 1, 0, 9, 7, 7, 9, 7, 5, 7, 5, 6, 9, 6, 1, 0, 10]
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
'''
4.1.10 Numpy數據結構
v = [0.5, 0.75, 1, 1.5, 2.0]
m = [v, v, v]
print(m)
'''
[[0.5, 0.75, 1, 1.5, 2.0],
[0.5, 0.75, 1, 1.5, 2.0],
[0.5, 0.75, 1, 1.5, 2.0]]
'''
print(m[1]) # [0.5, 0.75, 1, 1.5, 2.0]
print(m[1][0]) # 0.5
v1 = [0.5, 1.5]
v2 = [1, 2]
m = [v1, v2]
c = [m, m]
print(c) # [[[0.5, 1.5], [1, 2]], [[0.5, 1.5], [1, 2]]]
print(c[1][1][0]) # 1
# 引用指針的變化
v = [0.5, 0.75, 1, 1.5, 2.0]
m = [v, v, v]
print(m)
'''
[[0.5, 0.75, 1, 1.5, 2.0],
[0.5, 0.75, 1, 1.5, 2.0],
[0.5, 0.75, 1, 1.5, 2.0]]
'''
v[0] = 'python'
print(m)
'''
[['python', 0.75, 1, 1.5, 2.0],
['python', 0.75, 1, 1.5, 2.0],
['python', 0.75, 1, 1.5, 2.0]]
'''
# 實用copy可以避免
from copy import deepcopy
v = [0.5, 0.75, 1, 1.5, 2]
m = 3 * [deepcopy(v), ]
print(m)
'''
[[0.5, 0.75, 1, 1.5, 2],
[0.5, 0.75, 1, 1.5, 2],
[0.5, 0.75, 1, 1.5, 2]]
'''
v[0] = 'python'
print(m)
'''
[[0.5, 0.75, 1, 1.5, 2],
[0.5, 0.75, 1, 1.5, 2],
[0.5, 0.75, 1, 1.5, 2]]
'''
###常規Numpy數組
import numpy as np
a = np.array([0, 0.5, 1, 1.5, 2])
print(type(a)) # <class 'numpy.ndarray'>
# a[m:n] 從index m到index m-1
print(a[1:3]) # [0.5 1. ]
print(a.sum()) # 5.0
print(a.std()) # 標準差0.7071067811865476
print(a.cumsum()) # 運行累積和 [0. 0.5 1.5 3. 5. ]
# 向量化數學運算
print(a * 2) # [0. 1. 2. 3. 4.]
print((a ** 2)) # [0. 0.25 1. 2.25 4. ]
print(np.sqrt(a)) # [0. 0.70710678 1. 1.22474487 1.41421356]
b = np.array([a, a * 2])
print(b)
'''
[[0. 0.5 1. 1.5 2. ]
[0. 1. 2. 3. 4. ]]
'''
print(b[0]) # [0. 0.5 1. 1.5 2. ]
print(b[0][2]) # 1.0
print(b[0, 2]) # 1.0
print(b.sum())
4.1.11 結構數組
import numpy as np
dt = np.dtype([('Name', 'S10'),
('Age', 'i4'),
('Height', 'f'),
('Children/Pets', 'i4', 2)])
s = np.array([('Smith', 45, 1.83, [0, 1]),
('Jone', 53, 1.72, [2, 2])], dtype=dt)
print(s) #[(b'Smith', 45, 1.83, [0, 1]) (b'Jone', 53, 1.72, [2, 2])]
4.1.12 代碼向量化
import numpy as np
r = np.random.standard_normal((4, 3)) # 4行3列
print(r)
'''
[[-0.29598219 0.76700604 1.15793449]
[-1.08105824 0.82557973 -0.30853888]
[ 0.75692809 0.36453107 -0.21408086]
[ 0.45777808 0.36691887 0.95685358]]
'''
s = np.random.standard_normal((4, 3))
print(s)
'''
[[-0.35294214 -1.19310506 -0.70103347]
[-1.02810043 0.08575024 -1.24281986]
[ 0.36173324 0.44814883 0.80394147]
[ 0.67534743 0.86099954 -0.91206579]]
'''
print(r + s)
'''
[[ 0.43125362 -0.52441329 1.19103867]
[-0.2854767 -1.23106115 -0.22018792]
[ 0.20424545 0.90440544 0.4378501 ]
[-0.72101699 -1.33268177 -1.44506587]]
'''
t = 2 * r + 3
print(t)
# 必須是相同維數組才能計算
s = np.random.standard_normal(4)
# print(r+s) #ValueError: operands could not be broadcast together with shapes (4,3) (4,)
# 將r轉置
print(r.transpose() + s)
'''
[[-1.90960139 1.84034412 -1.35939014 2.45903169]
[-0.46102826 1.22686342 -1.57475201 2.04136837]
[ 1.79116583 1.17613761 -0.10087791 0.2849593 ]]
'''
x = np.random.standard_normal((5, 1000000))
y = 2 * x + 3 # 線性表達式y = 2*x+3
C = np.array((x, y), order='C')
F = np.array((x, y), order='F')
x = 0.0
y = 0.0 # 內存清零
print(C[:2].round(2))
[[[ 1.65 -0.31 0.01 ... 0.07 0.4 2.62]
[-0.51 -2.22 -0.18 ... 1.27 -0.43 0.04]
[-0.7 0.03 -1.06 ... -0.78 -0.56 -0.65]
[-1.1 -0.78 -0.09 ... 0.72 2.95 1.51]
[-0.97 -0.82 0.31 ... 0.2 0.71 -0.88]]
[[ 6.29 2.39 3.03 ... 3.15 3.79 8.24]
[ 1.97 -1.43 2.63 ... 5.55 2.13 3.07]
[ 1.6 3.06 0.88 ... 1.44 1.87 1.71]
[ 0.8 1.43 2.82 ... 4.45 8.9 6.02]
[ 1.05 1.36 3.62 ... 3.41 4.42 1.23]]]