如果你正在创建一个1d数组,你可以将它实现为一个列表,或者使用标准库中的'array'模块。我一直用链表来表示一维数组。

我想要使用数组模块的原因或情况是什么?

它是为了性能和内存优化,还是我遗漏了一些明显的东西?


当前回答

在性能方面,这里有一些比较python列表、数组和numpy数组的数字(都是2017年Macbook Pro上的python 3.7)。 最终的结果是python列表对于这些操作是最快的。

# Python list with append()
np.mean(timeit.repeat(setup="a = []", stmt="a.append(1.0)", number=1000, repeat=5000)) * 1000
# 0.054 +/- 0.025 msec

# Python array with append()
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a.append(1.0)", number=1000, repeat=5000)) * 1000
# 0.104 +/- 0.025 msec

# Numpy array with append()
np.mean(timeit.repeat(setup="import numpy as np; a = np.array([])", stmt="np.append(a, [1.0])", number=1000, repeat=5000)) * 1000
# 5.183 +/- 0.950 msec

# Python list using +=
np.mean(timeit.repeat(setup="a = []", stmt="a += [1.0]", number=1000, repeat=5000)) * 1000
# 0.062 +/- 0.021 msec

# Python array using += 
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a += array.array('f', [1.0]) ", number=1000, repeat=5000)) * 1000
# 0.289 +/- 0.043 msec

# Python list using extend()
np.mean(timeit.repeat(setup="a = []", stmt="a.extend([1.0])", number=1000, repeat=5000)) * 1000
# 0.083 +/- 0.020 msec

# Python array using extend()
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a.extend([1.0]) ", number=1000, repeat=5000)) * 1000
# 0.169 +/- 0.034

其他回答

Basically, Python lists are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time. If you need to shrink and grow your list time-efficiently and without hassle, they are the way to go. But they use a lot more space than C arrays, in part because each item in the list requires the construction of an individual Python object, even for data that could be represented with simple C types (e.g. float or uint64_t).

数组。另一方面,数组类型只是C数组的精简包装。它只能保存同构数据(也就是说,所有的数据类型都是相同的),因此它只使用sizeof(一个对象)* length字节的内存。通常,当需要将C数组公开给扩展或系统调用(例如,ioctl或ftnl)时,应该使用它。

数组中。array也是Python 2中表示可变字符串的合理方式。x(数组('B',字节))。然而,Python 2.6+和3。X提供了一个可变字节字符串bytearray。

但是,如果您想在数值数据的齐次数组上进行数学运算,那么最好使用NumPy,它可以自动向量化复杂多维数组上的操作。

长话短说:数组。当你出于数学以外的原因需要一个同构C数据数组时,数组是很有用的。

我的理解是数组更有效地存储(例如,作为连续的内存块与指向Python对象的指针相比),但我不知道有任何性能上的好处。此外,使用数组必须存储相同类型的原语,而列表可以存储任何类型的原语。

在性能方面,这里有一些比较python列表、数组和numpy数组的数字(都是2017年Macbook Pro上的python 3.7)。 最终的结果是python列表对于这些操作是最快的。

# Python list with append()
np.mean(timeit.repeat(setup="a = []", stmt="a.append(1.0)", number=1000, repeat=5000)) * 1000
# 0.054 +/- 0.025 msec

# Python array with append()
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a.append(1.0)", number=1000, repeat=5000)) * 1000
# 0.104 +/- 0.025 msec

# Numpy array with append()
np.mean(timeit.repeat(setup="import numpy as np; a = np.array([])", stmt="np.append(a, [1.0])", number=1000, repeat=5000)) * 1000
# 5.183 +/- 0.950 msec

# Python list using +=
np.mean(timeit.repeat(setup="a = []", stmt="a += [1.0]", number=1000, repeat=5000)) * 1000
# 0.062 +/- 0.021 msec

# Python array using += 
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a += array.array('f', [1.0]) ", number=1000, repeat=5000)) * 1000
# 0.289 +/- 0.043 msec

# Python list using extend()
np.mean(timeit.repeat(setup="a = []", stmt="a.extend([1.0])", number=1000, repeat=5000)) * 1000
# 0.083 +/- 0.020 msec

# Python array using extend()
np.mean(timeit.repeat(setup="import array; a = array.array('f')", stmt="a.extend([1.0]) ", number=1000, repeat=5000)) * 1000
# 0.169 +/- 0.034

标准库数组对于二进制I/O非常有用,例如将整型数组转换为要写入wave文件的字符串。也就是说,正如许多人已经注意到的,如果你要做任何实际工作,那么你应该考虑使用NumPy。

这是一种交易!

各有优点:

list

灵活的 可以是异质的

数组(例如:numpy数组)

统一值数组 均匀 紧凑(尺寸) 高效(功能和速度) 方便