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

查看:67
本文介绍了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))

推荐答案

list 的列表与二维 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天全站免登陆