Python一些基础知识
来源:互联网 发布:一句话介绍自己 知乎 编辑:程序博客网 时间:2024/05/21 00:45
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 Aprint Cprint "############################"print A3print "############################"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.flatprint 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=gbkfrom pickle import load,dumplist1 = ['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 1del 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=1print 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来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
0 0
- python的一些基础知识
- 一些python基础知识
- Python的一些基础知识
- Python一些基础知识
- Python的一些基础知识
- python的一些基础知识
- Python的一些基础知识
- python的一些基础知识
- 一些关于Python的基础知识
- 初学python的一些基础知识
- Python异常处理方面的一些基础知识。
- Python的一些基础知识(一)
- 一些基础知识
- 一些基础知识
- 一些基础知识
- 一些基础知识
- 一些基础知识
- 一些基础知识
- 14. Longest Common Prefix
- 机器学习基石笔记6——为什么机器可以学习(2)
- 科学计数法表示零
- MySQL在Window命令行中中文显示不对齐和乱码问题
- FTPClient 上传文件调用storeFile方法SocketException问题
- Python一些基础知识
- 百度:度度熊有一个N个数的数组,他想将数组从大到小排好序...
- 解决 Android N 7.0 上 报错:android.os.FileUriExposedException
- bzoj1115[POI2009]石子游戏Kam 阶梯NIM
- parse shell传递的参数
- Android 7.0 FileUriExposedException 解决
- ios ipa
- Lua与C#调用原理
- Android中StringBuilder与 StringBuffer区别