python小笔记

Python中容易混淆知识点的一些总结笔记


len, count, size, shape, ndim

len() -> 返回长度,e.g. len([1,2]) = 2, len([[1,2,3],[3,4,5]]) = 2

count() -> 计算个数, e.g. 'aabcc'.count('a') = 2

shape() 和 size() 是numpy中才有的函数

shape() -> 矩阵每维大小,e.g. np.shape(a) = (2,3) 2行3列

size() -> 数组元素总个数,相当于各维数之积,e.g. np.size(a) = 2*3 = 6

shape和size既可以作为函数,也可以作为ndarray的属性

a.shape ->(2,3) 作为属性不用括号调用的方法

ndim() -> 矩阵维度,e.g. np.ndim(a) = 2

原文地址:https://www.cnblogs.com/zzzzy/p/8490773.html