arrays - Python 增加数组的一部分

标签 arrays python-2.7 numpy

我想尽快增加 python 中数组的一部分。我使用一个简单的循环:

>>> test = [0,0,0,0,0]
>>> for i in xrange(1, 3):
    test[i] += 1

>>> test
[0,1,1,0,0]

在我的程序中,测试列表包含数百万个元素。也许 numpy 可以是解决方案?

谢谢

马克

最佳答案

NumPy 确实是一个解决方案:

import numpy as np
arr = np.array(test)
arr[1:3] += 1

如果您确实需要返回列表,则可以使用arr.tolist(),但更好的是从一开始就使用NumPy数组,无论在哪里你得到你的数据。

关于arrays - Python 增加数组的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26419451/

相关文章:

javascript - 按索引删除数组

python - Pandas :Read xlsx file to dict with column1 as key and column2 as values

python - 为什么 dict 没有在 python 多处理中更新?

python - 文件包含\u00c2\u00a0,转换为字符

python - 为 skimage 中的 regionprops 添加额外的属性

Python - 100万行表中日期的向量化差异

arrays - 优化对子数组执行的查询

arrays - 在 Swift 中处理多维对象数组

c++ - 如何交换数组中 Min 和 Max 的位置?

python - 使用 Numpy 数组而不是列表时排序算法中的递归错误