Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:复制一个`array.array`_Python_Arrays_Copy - Fatal编程技术网

Python:复制一个`array.array`

Python:复制一个`array.array`,python,arrays,copy,Python,Arrays,Copy,除了创建一个新列表并复制值,或者使用.to\u something和从某个对象复制值之外,还有什么方法可以在Python中复制列表(而不是列表)?我似乎在文档中找不到任何东西。如果没有,是否有类似的内置数据类型可以做到这一点 我正在研究一个高性能模块,所以答案越快越好 我目前的解决方案只是使用。to_bytes和。from_bytes,这比我的测试快了大约1.8倍。copy.copy(arr)可以很好地工作。不确定数组.array包含哪些内容,但使用示例: >>> import

除了创建一个新列表并复制值,或者使用
.to\u something
从某个对象复制值之外,还有什么方法可以在Python中复制列表(而不是
列表)?我似乎在文档中找不到任何东西。如果没有,是否有类似的内置数据类型可以做到这一点

我正在研究一个高性能模块,所以答案越快越好


我目前的解决方案只是使用
。to_bytes
。from_bytes
,这比我的测试快了大约1.8倍。

copy.copy(arr)
可以很好地工作。

不确定
数组.array
包含哪些内容,但使用示例:

>>> import array
>>> a = array.array('i', [1, 2, 3] * 1000)
array('i', [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
2, 3, 1, 2, 3, 1, 2, 3, 1, 2, ... ])
一些机构: 各种方法的计时 (在Jupyter笔记本中使用%timeit magic): 切片

In [1]: %timeit cp = a[:]

418 ns ± 4.89 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Deepcopy

In [2]: %timeit cp = deepcopy(a)

1.83 µs ± 34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit cp = np.copy(a)

1.87 µs ± 62.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[6]: pre = array.array('i', [0, 0, 0] * 1000)
In[7]: %timeit for i, element in enumerate(a): pre[i] = a[i]

344 µs ± 7.83 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
numpy复制。。。注意:这将生成一个numpy数组,而不是数组。数组

In [2]: %timeit cp = deepcopy(a)

1.83 µs ± 34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit cp = np.copy(a)

1.87 µs ± 62.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[6]: pre = array.array('i', [0, 0, 0] * 1000)
In[7]: %timeit for i, element in enumerate(a): pre[i] = a[i]

344 µs ± 7.83 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
列表理解和数组.数组转换

In [4]: %timeit cp = array.array('i', [item for item in a])

147 µs ± 5.39 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [5]: %timeit cp = array.array('i', np.copy(a))

310 µs ± 2.25 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
numpy copy and array.array conversion

In [4]: %timeit cp = array.array('i', [item for item in a])

147 µs ± 5.39 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [5]: %timeit cp = array.array('i', np.copy(a))

310 µs ± 2.25 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
复制到现有阵列

In [2]: %timeit cp = deepcopy(a)

1.83 µs ± 34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit cp = np.copy(a)

1.87 µs ± 62.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[6]: pre = array.array('i', [0, 0, 0] * 1000)
In[7]: %timeit for i, element in enumerate(a): pre[i] = a[i]

344 µs ± 7.83 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

您可以使用deepcopy和list-这是您想要的吗?
a[:]
怎么样?@Stefan测试了一下,它和我现在的方式差不多,但有时速度较慢。是的,idk为什么每个人都认为我指的是
list
s。你能展示一下你的测试吗?这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-@31皮它究竟是如何回答这个问题的?看起来你没有读过这个问题。那么这种“批评或要求澄清”是如何进行的呢?我提到的一个我发现更快的是
a*1
。我怀疑在引擎盖下它相当于
a[:]
,但它进入引擎盖下的速度更快(比如,它到达复制代码的速度更快:)。不过,只有小数组才值得注意。如果我没说错的话,
array.array
无法存储可变项。因此,使用
copy.deepcopy
而不是
copy.copy
是没有意义的。