迭代1到10次并在python中添加值和numpy数组的最佳方法

JPC*_*JPC -5 python arrays numpy

我知道这对某人来说是一个简单的问题,但我迷失在这里.

假设我希望我的结果是数组值中的10个随机数:

np.random.randint(0,2)  # returns 0,1
Run Code Online (Sandbox Code Playgroud)

我想迭代10次:

for i=1 to 10 : 
   np.random.randint(0,2) addto myarray
Run Code Online (Sandbox Code Playgroud)

我想要一个0,1,1,0...10次的numpy数组

谢谢.

DSM*_*DSM 7

阅读文档总是一个好主意.如果输入help(np.random.randint),您会看到:

randint(...)
    randint(low, high=None, size=None)

    Return random integers from `low` (inclusive) to `high` (exclusive).

    Return random integers from the "discrete uniform" distribution in the
    "half-open" interval [`low`, `high`). If `high` is None (the default),
    then results are from [0, `low`).

    Parameters
    ----------
    low : int
        Lowest (signed) integer to be drawn from the distribution (unless
        ``high=None``, in which case this parameter is the *highest* such
        integer).
    high : int, optional
        If provided, one above the largest (signed) integer to be drawn
        from the distribution (see above for behavior if ``high=None``).
    size : int or tuple of ints, optional
        Output shape. Default is None, in which case a single int is
        returned.
Run Code Online (Sandbox Code Playgroud)

第一个例子是:

Examples
--------
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
Run Code Online (Sandbox Code Playgroud)