问题:如何在python中获取任何大小的空数组?

我基本上想在C语言中使用python等效:

int a[x];

但在python中,我声明了一个数组,如:

a = []

问题是我想给随机槽分配值,例如:

a[4] = 1

但由于数组为空,我无法使用python做到这一点。

I basically want a python equivalent of this in C:

int a[x];

but in python I declare an array like:

a = []

and the problem is I want to assign random slots with values like:

a[4] = 1

but I can’t do that with python, since the array is empty.


回答 0

如果按“数组”实际上是指Python列表,则可以使用

a = [0] * 10

要么

a = [None] * 10

If by “array” you actually mean a Python list, you can use

a = [0] * 10

or

a = [None] * 10

回答 1

您不能完全按照Python的要求进行操作(如果我没看错的话)。您需要为列表的每个元素(或您所说的数组)放入值。

但是,请尝试以下操作:

a = [0 for x in range(N)]  # N = size of list you want
a[i] = 5  # as long as i < N, you're okay

对于其他类型的列表,也可以使用0以外的 None值。

You can’t do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).

But, try this:

a = [0 for x in range(N)]  # N = size of list you want
a[i] = 5  # as long as i < N, you're okay

For lists of other types, use something besides 0. None is often a good choice as well.


回答 2

您可以使用numpy:

import numpy as np

来自空数组的示例:

np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])  

You can use numpy:

import numpy as np

Example from Empty Array:

np.empty([2, 2])
array([[ -9.74499359e+001,   6.69583040e-309],
       [  2.13182611e-314,   3.06959433e-309]])  

回答 3

只需声明列表并附加每个元素即可。例如:

a = []
a.append('first item')
a.append('second item')

Just declare the list and append each element. For ex:

a = []
a.append('first item')
a.append('second item')

回答 4

您也可以使用list的extend方法扩展它。

a= []
a.extend([None]*10)
a.extend([None]*20)

also you can extend that with extend method of list.

a= []
a.extend([None]*10)
a.extend([None]*20)

回答 5

如果您(或该问题的其他搜索者)实际上对创建一个用整数填充的连续数组感兴趣,请考虑bytearraymemoryivew

# cast() is available starting Python 3.3
size = 10**6 
ints = memoryview(bytearray(size)).cast('i') 

ints.contiguous, ints.itemsize, ints.shape
# (True, 4, (250000,))

ints[0]
# 0

ints[0] = 16
ints[0]
# 16

If you (or other searchers of this question) were actually interested in creating a contiguous array to fill with integers, consider bytearray and memoryivew:

# cast() is available starting Python 3.3
size = 10**6 
ints = memoryview(bytearray(size)).cast('i') 

ints.contiguous, ints.itemsize, ints.shape
# (True, 4, (250000,))

ints[0]
# 0

ints[0] = 16
ints[0]
# 16

回答 6

x=[]
for i in range(0,5):
    x.append(i)
    print(x[i])
x=[]
for i in range(0,5):
    x.append(i)
    print(x[i])

回答 7

如果您确实想要C样式的数组

import array
a = array.array('i', x * [0])
a[3] = 5
try:
   [5] = 'a'
except TypeError:
   print('integers only allowed')

请注意,python中没有未初始化变量的概念。变量是绑定到值的名称,因此该值必须具有某些内容。在上面的示例中,数组以零初始化。

但是,这在python中并不常见,除非您实际上需要低级的东西。在大多数情况下,如其他答案所示,使用空列表或空numpy数组会更好。

If you actually want a C-style array

import array
a = array.array('i', x * [0])
a[3] = 5
try:
   [5] = 'a'
except TypeError:
   print('integers only allowed')

Note that there’s no concept of un-initialized variable in python. A variable is a name that is bound to a value, so that value must have something. In the example above the array is initialized with zeros.

However, this is uncommon in python, unless you actually need it for low-level stuff. In most cases, you are better-off using an empty list or empty numpy array, as other answers suggest.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。