在python中初始化一个固定大小的数组

kbb*_*kbb 141 python arrays list

我想知道如何初始化一个数组(或列表),但要用值填充,以具有定义的大小.

例如在C中:

int x[5]; /* declared without adding elements*/
Run Code Online (Sandbox Code Playgroud)

我如何在python中做到这一点?

谢谢.

sam*_*ias 188

您可以使用:

>>> lst = [None] * 5
>>> lst
[None, None, None, None, None]
Run Code Online (Sandbox Code Playgroud)

  • 问题指出"......尚未填充价值".Python文档表明"无"通常用于表示缺少值".我的例子以最短的代码量生成了这样一个定义大小的列表.在提出问题时,它是习惯用例Python中最接近的东西. (8认同)
  • 需要注意的一件事是:列表中的所有元素最初都将具有相同的 id(或内存地址)。当您稍后在代码中更改任何元素时,这只会影响指定的值,但是,如果您以这种方式创建自定义对象列表(使用 int 乘数),然后更改自定义对象的任何属性,这将更改列表中的所有对象。 (5认同)
  • 这符合问题的要求,因为你有一个已定义的大小数组,你可以索引五个元素中的一个而不会得到`IndexError`.这是与C表达式最接近的东西. (3认同)
  • 你可以做 lst = [0] * 5 ````>>> [0]*5``````[0, 0, 0, 0, 0]``` (3认同)
  • @user2233706 如果数组用零填充,它会更接近 C。 (2认同)

Pat*_*rin 73

为什么这些问题没有得到明显答案的回答?

a = numpy.empty(n, dtype=object)
Run Code Online (Sandbox Code Playgroud)

这将创建一个长度为n的数组,可以存储对象.它无法调整大小或附加到.特别是,它不会通过填充其长度来浪费空间.这是Java的等价物

Object[] a = new Object[n];
Run Code Online (Sandbox Code Playgroud)

如果您真的对性能和空间感兴趣并且知道您的数组只存储某些数字类型,那么您可以将dtype参数更改为其他值,如int.然后numpy将这些元素直接打包到数组中,而不是使数组引用int对象.

  • 如果它显而易见,那么其他人就会提供这个..再次,导入一个库只是为了获得这个功能可能不是最好的选择. (51认同)
  • 除了这是唯一的答案,这实际上是正确的.OP未指定禁止模块加载.他也没有要求一份清单.问题非常具体,这个答案是正确的.+1 (13认同)
  • 添加像numpy这样的大型模块是一种不必要的开销 (11认同)
  • 他们说的是Python,而不是Python + Numpy。Numpy 也不是一个简单的附加组件。它无法卸载,因此对于嵌入式 Python 使用来说是一个很大的问题。 (2认同)

TTi*_*imo 26

做这个:

>>> d = [ [ None for y in range( 2 ) ] for x in range( 2 ) ]
>>> d
[[None, None], [None, None]]
>>> d[0][0] = 1
>>> d
[[1, None], [None, None]]
Run Code Online (Sandbox Code Playgroud)

其他解决方案将导致这种问题:

>>> d = [ [ None ] * 2 ] * 2
>>> d
[[None, None], [None, None]]
>>> d[0][0] = 1
>>> d
[[1, None], [1, None]]
Run Code Online (Sandbox Code Playgroud)

  • 他问的是 Array 而不是 List (2认同)

laf*_*ras 12

最好的办法是使用numpy库.

from numpy import ndarray

a = ndarray((5,),int)
Run Code Online (Sandbox Code Playgroud)

  • 为了使用数组而导入库并不是最好的.存在其他方式. (16认同)
  • 最好吗? (2认同)

Ale*_*ler 9

一个简单的解决方案是x = [None]*length,但请注意它将所有列表元素初始化为None.如果尺寸确实是固定的,你也可以这样做x=[None,None,None,None,None].但严格地说,你不会得到未定义的元素,因为Python中不存在这种瘟疫.

  • 对于大多数C使用数组的cass,正常(非固定)列表是惯用的python等价物.这是问题的答案,因为它可能最有助于OP(从C转换到Python). (4认同)

kaa*_*men 8

>>> import numpy
>>> x = numpy.zeros((3,4))
>>> x
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> y = numpy.zeros(5)   
>>> y
array([ 0.,  0.,  0.,  0.,  0.])
Run Code Online (Sandbox Code Playgroud)

x是2-d阵列,y是1-d阵列.它们都用零初始化.

  • 您应该提供一种解释... (2认同)

lit*_*nce 7

>>> n = 5                     #length of list
>>> list = [None] * n         #populate list, length n with n entries "None"
>>> print(list)
[None, None, None, None, None]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, None, None, 1]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, None, 1, 1]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[None, None, 1, 1, 1]
Run Code Online (Sandbox Code Playgroud)

或者列表中没有任何内容可以开头:

>>> n = 5                     #length of list
>>> list = []                 # create list
>>> print(list)
[]

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[1]
Run Code Online (Sandbox Code Playgroud)

在附加的第四次迭代:

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[1,1,1,1]
Run Code Online (Sandbox Code Playgroud)

5以及随后的所有:

>>> list.append(1)            #append 1 to right side of list
>>> list = list[-n:]          #redefine list as the last n elements of list
>>> print(list)
[1,1,1,1,1]
Run Code Online (Sandbox Code Playgroud)