如何在数组末尾添加元素?

我有这段代码为所有从0到512的数字创建了一个二进制表示的数组,我想在每个数组的末尾添加一个随机值。随机值可以是0或1。这是生成数组的代码:

import numpy as np
POP_SIZE = 50
def initialize_population():
    pixels = 9
    population = []
    for i in range(POP_SIZE):
        chromosome = np.array([list((np.binary_repr(x, pixels))) for x in range(2 ** pixels)], dtype=int)
        population.append(chromosome)
    return population

I know that I could use np.append(chromosome, np.random.randint(2)) but I do not know how to integrate with the previos code.

评论