android diag无标题,无标题文章

Jun 05, 2016

@classmethod 的用法

通常情况下,要调用一个类,需要将其实例化,进而调用。

如果使用 @classmethod 就简单了。

class Hello:

def __init__:

pass

@classmethod

def print_hello(cls):

print 'Hello'

使用时直接

>>> Hello.print_hello()

Hello

sys.argv[] 的用法

获取命令行参数

sys.argv[0] 表示当前文件名

sys.argv[1] 表示第一个参数

比如命令行执行

python test.py --version

test.py 就是 sys.argv[0]

version 就是 sys.argv[1][2:]

Jun 18, 2016

np.meshgrid 的用法

接受两个一维数组,产生两个二维数组

>>>X = np.linspace(0, 1, 3)

>>>Y = np.linspace(0, 1, 2)

>>>xx, yy = np.meshgrid(X, Y)

>>>xx

array([[ 0. , 0.5, 1. ],

[ 0. , 0.5, 1. ]])

>>>yy

array([[ 0., 0., 0.],

[ 1., 1., 1.]])

将 X 重复两次,将 Y 变纵向重复三次,这样利于网格计算

>>>z = xx2 + yy2

array([[ 0. , 0.25, 1. ],

[ 1. , 1.25, 2. ]])

效果相当于以原来X,Y为坐标进行计算

np.where

np.where(cond, x, y)

其中 cond, x, y都可以是数组,cond为真取 x, 为假取 y

np.linalg 函数

diag 返回对角线元素

dot 矩阵乘法

det 行列式值

inv 逆

solve 解线性方程组

np.random

randn 平均值0标准差1的正态分布

normal 正态分布

rand 均匀分布

randint 给定上下限随机选取整数

str.join()

与 split() 用法正好相反

>>>words = ['This', 'is', 'an', 'example']

>>>''.join(words)

'Thisisanexample'

>>>' '.join(words)

'This is an example'

字典推导式

可能你见过列表推导时,却没有见过字典推导式,在2.7中才加入的:

d = {key: value for (key, value) in iterable}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值