【问题标题】:Add an element to the list after x of N rounds在 N 轮中的 x 次后向列表中添加一个元素
【发布时间】:2021-01-14 10:44:55
【问题描述】:

假设我在 python 中有以下列表:

probs = [0.1, 0.4, 0.9]

实验次数为 20(N_episodes),经过 x 次实验,例如10、我想在列表中添加一个元素。

我已经想出了一个 for 循环:

for i in range(N_episodes):
        new_bandit = np.random.random()
        new_bandit_probs = probs.append(new_bandit)

但这给了我无穷无尽的新元素,我只想要一个新元素。有人可以帮帮我吗?

【问题讨论】:

    标签: python list insert add


    【解决方案1】:

    你大概可以用这个:

    x = 10 # The number of experiments after which you need to add number.
    for i in range(N_episodes):
        if i == x:
            new_bandit = np.random.random()
            new_bandit_probs = probs.append(new_bandit)
    

    【讨论】:

      猜你喜欢
      • 2023-02-10
      • 1970-01-01
      • 2018-08-11
      • 2015-11-10
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2011-12-04
      • 2013-12-05
      相关资源
      最近更新 更多