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

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

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


当前回答

这个答案将总结几乎所有关于何时使用List和Array的查询:

The main difference between these two data types is the operations you can perform on them. For example, you can divide an array by 3 and it will divide each element of array by 3. Same can not be done with the list. The list is the part of python's syntax so it doesn't need to be declared whereas you have to declare the array before using it. You can store values of different data-types in a list (heterogeneous), whereas in Array you can only store values of only the same data-type (homogeneous). Arrays being rich in functionalities and fast, it is widely used for arithmetic operations and for storing a large amount of data - compared to list. Arrays take less memory compared to lists.

其他回答

数组模块是一种如果你不知道为什么要使用它,你可能不需要的东西(请注意,我并不是试图以一种居高居高下的方式说!)。大多数情况下,数组模块用于与C代码进行交互。为了更直接地回答你关于性能的问题:

在某些用途上,数组比列表更有效。如果你需要分配一个你知道不会改变的数组,那么数组可以更快,使用更少的内存。GvR有一个优化的轶事,其中数组模块是赢家(长读,但值得一读)。

另一方面,列表比数组占用更多内存的部分原因是,当所有分配的元素都被使用时,python会分配一些额外的元素。这意味着向列表中添加项更快。所以,如果你打算添加项目,列表是最好的方法。

我只会在有特殊的优化需求或者需要与C代码交互(不能使用pyrex)时使用数组。

对于几乎所有情况,普通列表都是正确的选择。arrays模块更像是C数组上的一个薄包装器,它为您提供了一种强类型容器(参见文档),可以访问更多类似C的类型,如有符号/无符号short或double,这些都不是内置类型的一部分。我会说,只有在你真的需要它的时候才使用数组模块,在所有其他情况下,坚持使用列表。

在性能方面,这里有一些比较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

这个答案将总结几乎所有关于何时使用List和Array的查询:

The main difference between these two data types is the operations you can perform on them. For example, you can divide an array by 3 and it will divide each element of array by 3. Same can not be done with the list. The list is the part of python's syntax so it doesn't need to be declared whereas you have to declare the array before using it. You can store values of different data-types in a list (heterogeneous), whereas in Array you can only store values of only the same data-type (homogeneous). Arrays being rich in functionalities and fast, it is widely used for arithmetic operations and for storing a large amount of data - compared to list. Arrays take less memory compared to lists.

这是一种交易!

各有优点:

list

灵活的 可以是异质的

数组(例如:numpy数组)

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