gpt4 book ai didi

python - 使用 numpy 或其他库在 python 中进行列表扩充

转载 作者:行者123 更新时间:2023-12-01 01:08:31 25 4
gpt4 key购买 nike

我想增加列表

[1, 2, 3, 4, 5]

[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]

如果我想同样增强n次(比如100或500倍),我该怎么做?我不想用常规循环来做到这一点,而是使用像 numpy 这样的库。有什么帮助吗?

非常感谢。

最佳答案

您可以使用 numpy 的 np.repeat 来做到这一点:

import numpy as np
a = np.array([1, 2, 3, 4, 5])
np.repeat(a,2)
# array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])

关于python - 使用 numpy 或其他库在 python 中进行列表扩充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55098643/

25 4 0
文章推荐: python - 优化旋转和填充
文章推荐: javascript - 寻找有关 React 事件上的 bind(this, id) 的说明
文章推荐: JavaScript 自动解码 PHP 通过 HTML
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com