持续更新...
1.split函数
split是分割函数,将字符串分割成“字符”。
默认不带参数为空格分割。可以带参数根据实际需求进行分割。数字参数,表示“切几次”。
2.strip函数
note:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
当rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' ');
符号 意义
\n 换行(newline)
\r 回车CR (return)
\t TAB(制表符)
rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉;若不在开头或结尾处,则不删掉。
3.关闭图像
fig = plt.figure() # 新图
plt.savefig() # 保存
plt.close('all') # 关闭图
4.linspace 等差数列
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
在指定的间隔内返回均匀间隔的数字。
5.asarray转化为数组
numpy.
asarray
(a, dtype=None, order=None)
Convert the input to an array.
6.数组
Python的数组分三种类型:
(1) list 普通的链表,初始化后可以通过特定方法动态增加元素。
定义方式:arr = [元素]
(2) Tuple 固定的数组,一旦定义后,其元素个数是不能再改变的。
定义方式:arr = (元素)
(2) Dictionary 词典类型, 即是Hash数组。
定义方式:arr = {元素k:v}
7.ravel
numpy.
ravel
(a, order='C')
Return a contiguous flattened array.
numpy中的ravel()、flatten()、squeeze()都有将多维数组转换为一维数组的功能,区别:
ravel():如果没有必要,不会产生源数据的副本
flatten():返回源数据的副本
squeeze():只能对维数为1的维度降维
8.字串格式化
格式化字串時,所使用的%d、%f、%s等 與C語 言類似,之後使用%接上一個tuple, 也就是範例中以()包 括的實字表示部份。一些可用的字串格式字列舉如下:
%% | 在字串 中顯示% |
%d | 以10 進位整數方式輸出 |
%f | 將浮點 數以10進位方式輸出 |
%e, %E | 將浮點 數以10進位方式輸出,並使用科學記號 |
%o | 以8進 位整數方式輸出 |
%x, %X | 將整 數以16進位方式輸出 |
%s | 使用str()將字串輸出 |
%c | 以字元 方式輸出 |
%r | 使用repr()輸 出字串 |
9.enumerate() 函数
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
普通的 for 循环:
>>>i = 0 >>>
seq = ['one', 'two', 'three'] >>>
for element in seq: ...
print i, seq[i] ... i +=1 ... 0 one 1 two 2 three
for 循环使用 enumerate:
>>>seq = ['one', 'two', 'three'] >>>
for i, element in enumerate(seq): ...
print i, element ... 0 one 1 two 2 three
10.矩阵乘法
np.dot(A, B):计算真正意义上的矩阵乘积,同线性代数中矩阵乘法的定义。
实现对应元素相乘,有2种方式,一个是np.multiply(),另外一个是*。
# 对应元素相乘 element-wise product
element_wise = two_dim_matrix_one * another_two_dim_matrix_one
# 对应元素相乘 element-wise product
element_wise_2 = np.multiply(two_dim_matrix_one, another_two_dim_matrix_one)
11.数组切片
对于X[:,0];
是取二维数组中第一维的所有数据
对于X[:,1]
是取二维数组中第二维的所有数据
对于X[:,m:n]
是取二维数组中第m维到第n-1维的所有数据
12.numpy.concatenate
join a sequence of arrays along an existing axis.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html#numpy.concatenate
13.numpy.where
Return elements, either from x or y, depending on condition.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html