美文网首页
列表list、数组np.array等的len,size,shap

列表list、数组np.array等的len,size,shap

作者: spectre_hola | 来源:发表于2018-10-14 19:49 被阅读0次

本菜最近师命难违,在别人享受大四生活的同时不得不学习代码,搞搞DL。python基础差实在是难受,本菜记忆力和金鱼差不多,故写下这些小知识点以便常常复习之用,希望大佬看到不要踩我

参考原博:https://blog.csdn.net/Alicehzj/article/details/78686293


python中常见的二维数组有list与numpy.array。在很多情况下我们需要获取数组的大小,阅读过一些python代码可以发现,常见的方法一般有len, size, shape这三种,那么这三种方法分别应用于那些场合?有什么区别?

import numpy as np
a = [[1,2,3,4], [5,6,7,8], [9, 10, 11, 12]]
b = np.array(a)
print type(a)
print a
print type(b)
print b


<type 'list'>
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
<type 'numpy.ndarray'>
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

list

list---len

print len(a), len(a[0])


3 4

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-2f7fbe06ffd7> in <module>()
      1 print len(a), len(a[0])
----> 2 print size(a)

NameError: name 'size' is not defined

list---size

print size(a)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-17932fb9dbd4> in <module>()
----> 1 print size(a)

NameError: name 'size' is not defined


In [6]:
print a.size


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-d6180f130c7b> in <module>()
----> 1 print a.size

AttributeError: 'list' object has no attribute 'size'

list---shape

print shape(a)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-5b1e858da0b7> in <module>()
----> 1 print shape(a)

NameError: name 'shape' is not defined



In [8]:
print a.shape


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-2f66fe9ba9f6> in <module>()
----> 1 print a.shape

AttributeError: 'list' object has no attribute 'shape'

由上可知,list只支持len(), 该方法实际是调用了对象的len(self)方法

numpy.array

对比之下,numpy.array同时支持len, size, shape, 注意看三者返回值的区别。

此外,numpy中还提供matrix的数据类型,具体请看:

c = np.mat(a)
print type(c)
print c
d = np.mat(b)
print type(d)
print d
print len(d)
print d.size
print d.shape


<class 'numpy.matrixlib.defmatrix.matrix'>
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
<class 'numpy.matrixlib.defmatrix.matrix'>
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
3
12
(3, 4)

从上面的例子可以看出,martix支持由list和numpy.array创建,同时支持len, size以及shape.

相关文章

  • 列表list、数组np.array等的len,size,shap

    本菜最近师命难违,在别人享受大四生活的同时不得不学习代码,搞搞DL。python基础差实在是难受,本菜记忆力和金鱼...

  • Python列表,元组

    1.列表 1.len/index/count/列表[索引]取值 len(list)取列表的长度list.index...

  • Day6作业

    1.已知一个列表,求列表中心元素。 def center(list):len1=len(list)if len1%...

  • Python完结篇_基础知识2

    Python数列 1. 函数 len(list)—列表元素个数 max(list)—列表最大值 min(list...

  • python list中的方法和函数

    len(list) 列表元素的个数 max(list) 返回列表的最大值 min(list) 返回列表的最小值 l...

  • Python学习笔记(三)集合

    list & tuple list列表——有序集合 len(listname) 获取list元素个数 IndexE...

  • day8-总结

    关于列表(list)的其他操作 1.修改列表元素:列表名[下标]=值 获取列表长度:len(list5) 2.列表...

  • 10.Python-Numpy总结

    Numpy 数组 用list和tuple等数据结构表示数组,从列表产生数组 a=array(list),或直接将列...

  • Python List len()方法

    len() 方法返回列表元素个数。 以下实例展示了 len()函数的使用方法: list1,list2=[123,...

  • 【函数学习】Python List len()

    len(list) 参数 list -- 要计算元素个数的列表。 返回值 返回列表元素个数。 参考与详细:官方文档...

网友评论

      本文标题:列表list、数组np.array等的len,size,shap

      本文链接:https://www.haomeiwen.com/subject/tgdtzftx.html