如何在 Python 中声明一个数组? [英] How to declare an array in Python?
问题描述
如何在 Python 中声明数组?
How do I declare an array in Python?
我在文档中找不到任何对数组的引用.
I can't find any reference to arrays in the documentation.
推荐答案
variable = []
现在变量
指的是一个空列表*.
Now variable
refers to an empty list*.
当然这是一个赋值,而不是一个声明.在 Python 中没有办法说这个变量不应该引用列表以外的任何东西",因为 Python 是动态类型的.
Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed.
*默认的内置 Python 类型称为 list,而不是数组.它是一个任意长度的有序容器,可以容纳异构的对象集合(它们的类型无关紧要,可以自由混合).这不应与 array
模块混淆,它提供了一种更接近 C array
类型的类型;内容必须是同质的(都是同一种类型),但长度仍然是动态的.
*The default built-in Python type is called a list, not an array. It is an ordered container of arbitrary length that can hold a heterogenous collection of objects (their types do not matter and can be freely mixed). This should not be confused with the array
module, which offers a type closer to the C array
type; the contents must be homogenous (all of the same type), but the length is still dynamic.
这篇关于如何在 Python 中声明一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!