您的位置:首页 > 编程语言 > Python开发

Python一些基础知识

2017-05-09 17:04 471 查看

python 中[…]

# coding=gbk
'''
Created on 2017年5月9日

'''
from scipy.misc.pilutil import * # read image ,read 会提示错误,但是不影响使用
import matplotlib.pyplot as plt   # show image
import numpy as np # 两个方法都用
from numpy import *
A = np.zeros((2,3),dtype='float')
C = np.zeros((2,3),dtype='float')
B = np.array([[1,2,3],[4,5,6]])

A3 = np.zeros((2,2,3),dtype='float')
C3 = np.zeros((2,2,3),dtype='float')

B3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])

A[0,...] = B[0,...]

C[0,:] = B[0,:]

A3[0,...] = B3[0,:]
C3[:] = B3[...]

print A
print C
print "############################"
print A3
print "############################"
print C3


result:

[[ 1.  2.  3.]
[ 0.  0.  0.]]
[[ 1.  2.  3.]
[ 0.  0.  0.]]
############################
[[[ 1.  2.  3.]
[ 4.  5.  6.]]

[[ 0.  0.  0.]
[ 0.  0.  0.]]]
############################
[[[  1.   2.   3.]
[  4.   5.   6.]]

[[  7.   8.   9.]
[ 10.  11.  12.]]]


结果[…]和[:]几乎具有同样的效果,其他情况需要待测。

arr.flat

arr1 = np.array([1,2,3])

b = np.zeros(arr1.shape)

b.flat = arr1.flat

print b  # [1,2,3]


词典dict的get函数

词典可以通过键值对访问,也可以通过get访问,区别是:如果元素不存在,通过键值对访问会提示错误,但是通过get返回none,当然也可以通过制定参数的形式,来返回你想要的结果。如下:

print dic1.get('xlh','你查找的内容不存在!')  #存在的时候,返回值
print dic1.get('gyl')                  #默认返回none,
print dic1.get('gyl','你查找的内容不存在!')  #不存在,返回指定的参数

temp = dic1.get('gyl')

if temp is not None:
pass # do something


pickle模块

# coding=gbk

from pickle import load,dump

list1 = ['xlh',20,'gyl','21']

with open('text.txt','w') as f:
dump(list1,f)
print 'save list1 to text1 file over ....'

with open('text2.txt','wb') as f2:
dump(list1,f2)
print 'save list1 to text2 file over ...'


python清理变量内存

清理变量所占的内存,有两种方法:

var = None; # method 1
del var   ; # method 2


方法1虽然会留一个变量名,单几乎不占内存,因此可以忽略。

dlib for python2.7安装

conda install -c menpo dlib=18.18


copy文件

import shutil
filename = 'helloworld.mat'
dstname  = 'D:/helloworld.mat'
shutil.copy(filename,dstname)
print 'copy successfully!'


flatten

from compiler.ast import flatten # 在python3.5下,废弃
from funcy import flatten, isa # 替代,去pypi下载funcy安装即可。


同样的代码在两个不同的环境有不同的结果时

当同样的代码在两个不同的环境有不同的结果时,请仔细检查两个环境中变量的定义的类型是否相同。

python执行字符串中的表达式和语句

eval:计算字符串中的表达式

exec:执行字符串中的语句

execfile:用来执行一个文件

# 执行表达式
x=1
print eval("x+1")
# 执行语句
exec('pose = np.zeros(shape=[10,2],dtype=np.float32)')
for i in range(npose):
exec('pose'+str(i)+'np.zeros(shape=[10,2],dtype=np.float32)')


hdf5数据的items()

访问hdf5里面有哪些属性字段

train_data = h5py.File(train_path)
print(train_data.items())


一维数组(64,)到二维矩阵(64,1)

>>> import numpy as np
>>> a = np.arange(5)
>>> a.shape
(5L,)
# 方式一:利用 np.expand_dims
>>> np.expand_dims(a, axis=1).shape
(5L, 1L)
# 方式二:利用 np.reshape
>>> np.reshape(a, (-1, 1)).shape
(5L, 1L)
# 方式三:利用 np.newaxis
>>> a[:, np.newaxis].shape
(5L, 1L)
# 方式四:直接操作 shape 属性
>>> b = a.copy()
>>> b.shape = (-1, 1)
>>> b.shape
(5L, 1L)
>>> a.shape
(5L,)

作者:采石工
链接:https://www.zhihu.com/question/59563149/answer/168674704
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


print 格式化矩阵

利用set_printoptions()设置输出矩阵的格式化形式

import numpy as np
x=np.random.random(10)
print(x)
# [ 0.07837821  0.48002108  0.41274116  0.82993414  0.77610352  0.1023732
#   0.51303098  0.4617183   0.33487207  0.71162095]

np.set_printoptions(precision=3)
print(x)
# [ 0.078  0.48   0.413  0.83   0.776  0.102  0.513  0.462  0.335  0.712]


获得当前程序的运行的路径

import os
path_dir = os.getcwd()


在assert后加入说明,

a = 100
assert a == 1000 , 'please make sure a == 1000'
assert mode in ['training', 'inference']


window 版本的 pythonlib

https://www.lfd.uci.edu/~gohlke/pythonlibs

python装饰器

详细参考知乎:

https://www.zhihu.com/question/26930016
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: