首页 > 代码库 > 『Python』常用函数实践笔记

『Python』常用函数实践笔记

库安装:

1).pip & conda

2).在win10下手动安装python库的方法:

『python』计算机视觉_OpenCV3库安装

原生:

list.append():添加元素到list末尾

list.extend():使用一个list扩展另一个list

字典列表化:字典是有顺序的,而且list字典等于list字典的key

dict = {‘c‘:1,‘b‘:2,‘a‘:3}

list(dict)
# Out[13]: 
# [‘c‘, ‘b‘, ‘a‘]

list(dict.keys())
# Out[12]: 
# [‘c‘, ‘b‘, ‘a‘]

 random库:

random.randrange ([start,] stop [,step])

# 输出 100 <= number < 1000 间的偶数
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)

# 输出 100 <= number < 1000 间的3的倍数加100的结果
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)

 

os库:

os.path.exists(sub_dir_path):判断是否存在文件夹

os.makedirs(sub_dir_path):创建文件夹

os.walk(’路径‘):递归遍历,返回所有子文件

[x[0] for x in os.walk(‘./‘)]
# Out[1]:
# [‘./‘, ‘./帮师兄修图‘, ‘./.idea‘, ‘./.idea/inspectionProfiles‘, ‘./Machine_Learning‘, ‘./Machinening # /1400OS_05_Codes‘, ‘./Machine_Learning/1400OS_01_Codes‘, # ...... # ‘./TensorFlow/迁移学习/bottleneck‘, ‘./TensorFlow/迁移学习/inception_dec_2015‘]

 os.path.join():合并各个字符串,添加’/‘为路径

os.path.join(‘dir1‘,‘dir2‘)
# Out[1]: 
# ‘dir1/dir2‘

os.path.basename(file_name):剔除路径,保存文件名

os.path.basename(‘/123/123‘)
Out[1]: 
‘123‘

 

glob库:

glob.glob(file):返回匹配的文件

glob.glob(./flower_photos/tulips/*.jpg)
# Out[1]:
# [‘./flower_photos/tulips/19425920580_cdc8f49aed_n.jpg‘, ‘./flower_photos/tulips # /17094167287_865840060d_n.jpg‘, ‘./flower_photos/tulips/8712282563_3819afb7bc.jpg‘, # ‘./flower_photos/tulips/7166606598_5d2cd307c3.jpg‘‘]

matplotlib.pyplot

中文详细教程

numpy库:

np.bincount()

numpy.bincount详解

np.maximum(X, Y, out=None):

    • X 与 Y 逐位比较取其大者;
    • 最少接收两个参数

np.squeeze():剔除长度为一的轴

np.squeeze(np.array([[1,2,3]]))
# Out[17]: 
# array([1, 2, 3])
np.squeeze(np.array([[1],[2],[3]]))
# Out[18]: 
# array([1, 2, 3])

np.nditer():numpy的强大迭代器

默认情况下,nditer将视待迭代遍历的数组为只读对象(read-only),为了在遍历数组的同时,实现对数组元素值得修改,必须指定op_flags=[‘readwrite‘]模式:

np.nditer(a, op_flags=[‘readwrite‘])

基本迭代参数flag=[‘f_index‘/‘mulit_index‘],可输出自身坐标it.index/it.multi_index:

a = np.arange(6).reshape(2,3)

‘‘‘迭代方式‘‘‘

# 单维迭代
it = np.nditer(a, flags=[‘f_index‘])
while not it.finished:
    print("%d <%s>" % (it[0], it.index))
    it.iternext()
    
#0 <0>
#1 <2>
#2 <4>
#3 <1>
#4 <3>
#5 <5>


# 多维迭代
it = np.nditer(a, flags=[‘multi_index‘])
while not it.finished:
    print("%d <%s>" % (it[0], it.multi_index))
    it.iternext()
    
#0 <(0, 0)>
#1 <(0, 1)>
#2 <(0, 2)>
#3 <(1, 0)>
#4 <(1, 1)>
#5 <(1, 2)>

 改变迭代的顺序:

# 列顺序迭代
it = np.nditer(a, flags=[‘f_index‘], order=‘F‘)
while not it.finished:
    print("%d <%s>" % (it[0], it.index), end=‘ | ‘)
    it.iternext()
    
# 0 <0> | 3 <1> | 1 <2> | 4 <3> | 2 <4> | 5 <5> | 


# 行顺序迭代
it = np.nditer(a, flags=[‘f_index‘], order=‘C‘)
while not it.finished:
    print("%d <%s>" % (it[0], it.index), end=‘ | ‘)
    it.iternext()
    
# 0 <0> | 1 <2> | 2 <4> | 3 <1> | 4 <3> | 5 <5> | 

 

深入理解numpy广播机制:

numpy计算函数返回默认是一维行向量:

import numpy as np

a = [[1,1,1],
    [2,2,2],
    [3,3,3]]
b = (np.sum(a,axis=1))
c = (np.sum(a,axis=0))
print(b,‘\n‘,c)

# [3 6 9] 
# [6 6 6]

 所以广播之实际是高维对一维行向量的广播:

除法广播:

b = a/(np.sum(a,axis=1))
c = a/(np.sum(a,axis=0))
print(b,‘\n‘,c)

# [[ 0.33333333  0.16666667  0.11111111]
#  [ 0.66666667  0.33333333  0.22222222]
#  [ 1.          0.5         0.33333333]] 
# [[ 0.16666667  0.16666667  0.16666667]
#  [ 0.33333333  0.33333333  0.33333333]
#  [ 0.5         0.5         0.5       ]]

 向量乘法,加法可以类比:

np.array([1,2,3])*np.array([1,1,1])
# [1 2 3]

np.array([1,2,3])*np.array([1])
# [1 2 3]

np.array([1,2,3])*np.array([1,1])
# 报错

np.array([[1],[1],[1]])*np.array([1,2,3])
# [[1 2 3]
#  [1 2 3]
#  [1 2 3]]

 

『Python』常用函数实践笔记