Python:多维数组(“矩阵")与列表中的列表是否相同? [英] Python: Is a multidimensional array ("matrix") the same thing as lists within lists?

查看:600
本文介绍了Python:多维数组(“矩阵")与列表中的列表是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解人们所谓的矩阵与人们称呼列表中的列表之间的区别.

I'm trying to understand the differences between what people call matrices and what people call lists within lists.

它们是否相同,一旦创建,您就可以对它们执行相同的操作(引用元素在它们内部的使用方式相同,等等).

Are they the same in that, once created, you can do identical things to them (reference elements the same way within them, etc).

示例:

在列表中制作列表:

ListsInLists = [[1,2],[3,4],[5,6]]

制作多维数组:

np.random.rand(3,2)

堆叠数组以形成矩阵:

Array1 = [1,2,3,4]
Array2 = [5,6,7,8]
CompleteArray = vstack((Array1,Array2))

推荐答案

列表列表与二维Numpy数组非常不同.

A list of list is very different from a two-dimensional Numpy array.

  • 列表具有动态大小,可以容纳任何类型的对象,而数组具有固定的大小和统一类型的条目.
  • 在列表列表中,每个子列表可以具有不同的大小.数组沿每个轴的尺寸都是固定的.
  • 数组存储在连续的内存块中,而列表中的对象可以存储在堆中的任何位置.

Numpy数组的限制更严格,但可以提供更高的性能和内存效率.它们还为向量化的数学运算提供方便的功能.

Numpy arrays are more restrictive, but offer greater performance and memory efficiency. They also provide convenient functions for vectorised mathematical operations.

在内部,列表表示为指向任意Python对象的指针数组.在列表末尾重复添加时,该数组使用指数型超分配来实现线性性能.另一方面,Numpy数组通常表示为C的数字数组.

Internally, a list is represented as an array of pointers that point to arbitrary Python objects. The array uses exponential over-allocation to achieve linear performance when appending repeatedly at the end of the list. A Numpy array on the other hand is typically represented as a C array of numbers.

(此答案未涵盖Numpy对象数组的特殊情况,该特殊情况也可以容纳任何类型的Python对象.由于它们受Numpy数组的限制,因此很少使用,但没有性能优势)

(This answer does not cover the special case of Numpy object arrays, which can hold any kind of Python object as well. They are rarely used, since they have the restrictions of Numpy arrays, but don't have the performance advantages.)

这篇关于Python:多维数组(“矩阵")与列表中的列表是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆