Python函数
2016-12-14 20:01
597 查看
一 函数说明
主要记录python编程中遇到的函数,方便查看。
二 函数
shape
读取矩阵的维度。如:
>>> a = array([[1,0,0,0],[0,1,0,0],[0,0,1,0]]) >>> a array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]) >>> b.shape[0] 3 >>> b.shape[1] 4
sort sorted和argsort
1.sort方法仅定义在list中,排序时会影响列表本身>>> a = [2,0,1,6,1,2,1,4] >>> a.sort() >>> a [0, 1, 1, 1, 2, 2, 4, 6]
2.sorted()对所有可迭代序列都可以排序,针对任何可迭代序列都是反回一个list
>>> print(sorted({2:'p',0:'y',1:'t',6:'h',7:'o',8:'n'})) [0, 1, 2, 6, 7, 8] sorted(iterable, cmp, key, reverse)
iterable:是可迭代类型;
cmp:用于比较的函数,比较什么由key决定;
key:用于列表元素的某个属性或函数进行作为关键字,有默认值,地带集合中的一项;
reverse:排序规则,reverse=True降序或者reverse=False升序,有默认值
返回值是一个经过排序的可迭代类型,与iterable一样
sum
sum是求和函数。sum的参数是一个list,可以求一个矩阵的和也可以按行和列求和。
>>> b.sum() 3 >>> b.sum(axis=0) array([1, 1, 1, 0]) >>> b.sum(axis=1) array([1, 1, 1])
tile
重复某个数组,tile(a,(m,n))
>>> tile(a,2) array([[1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0]]) >>> tile(a,(2,1)) array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]])
本人也是初学python如有错误请指正。
相关文章推荐
- 《扩展和嵌入python解释器》1.6 从C中调用Python函数
- 在一个函数中如何知道调用者(Python)
- 有关 Python 中的 File.readlines(sizehint) 函数
- Python中动态添加类的属性或成员函数
- Python 函数、文件与模块
- Python函数中接受元组和字典
- python ip和int 互转函数
- python写的随机生成身份证号码的函数
- Wonderful String Translation Function in Python / Python神奇的字符串变换函数
- python速查笔记(1) 类型与函数
- Python Cookbook 第二版 汉化版 [Recipe 16.4] 将参数与函数关联起来(Currying)
- python函数
- Python小程序解析之--函数 powersum
- python函数参数
- 可爱的 Python:Python 中的函数编程
- [Python]Python函数参数中的*,**
- Python语言的数学函数
- Python中函数的参数定义和可变参数
- Python Cookbook 第二版 汉化版 [Recipe 16.4] 将参数与函数关联起来(Currying)
- Python的函数参数传递:传值?引用?