【问题标题】:list augmentation in python with numpy or other library [duplicate]使用numpy或其他库在python中进行列表扩充[重复]
【发布时间】:2019-08-01 13:51:27
【问题描述】:

我想扩充列表来自

[1, 2, 3, 4, 5]

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

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

非常感谢。

【问题讨论】:

  • 你想要列表中的每个数字两次
  • 只有 itertools 你可以做到list(chain.from_iterable(repeat(x, 2) for x in a))
  • user3483203 将我的问题标记为重复,答案就是我想知道的。 yatu也回答了我的问题;谢谢大家。

标签: python list numpy data-augmentation


【解决方案1】:

您可以使用 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])

【讨论】:

  • 请像这样标记明显的重复项,而不是回答。
  • user3483203 // 我预计它会重复,但我问这个问题只是因为我找不到与我认为相关的关键字的关键字。很抱歉问了一个重复的问题。
猜你喜欢
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 2022-10-07
  • 2021-08-02
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多