列表list、数组np.array等的len,size,shape操作

本菜最近师命难违,在别人享受大四生活的同时不得不学习代码,搞搞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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基础篇NumPy的主要对象是同种元素的多维数组。这是一个所有的元素都是一种类型、通过一个正整数元组索引的元素表格(...
    oyan99阅读 5,164评论 0 18
  • 先决条件 在阅读这个教程之前,你多少需要知道点python。如果你想从新回忆下,请看看Python Tutoria...
    舒map阅读 2,611评论 1 13
  • 《月亮与六便士》是‘’天才小说家‘’毛姆的代表作,毛姆是英国小说家、戏剧家。著名的有戏剧《圈子》,长篇小说《人性的...
    深谷留风阅读 4,437评论 38 103
  • 我是80后,姐弟三个,爸妈是下岗大潮中的一员。 我是广东人,广东人重男轻女不必避讳;所幸我是客家人,客家女性能撑半...
    彩凤知音阅读 365评论 1 1
  • 羁绊往往是我最不想遇到的,它会牵制我们的行动,思想。 夜晚的风温和清凉,我在家待的久了,超会趁夜晚的些...
    希GX阅读 717评论 1 0