numpy.random.randint不会以逗号返回列表separte

时间:2016-03-08 17:06:19

标签: python numpy scipy

我正在运行此代码:

import numpy as np
Z=np.ones(10)
I = np.random.randint(0,len(Z),20).
print I 

#[9 0 0 1 0 2 3 4 3 3 2 2 7 8 1 9 9 2 1 7]

#so this instruction does not work
print Z[I]

返回一个列表,如果元素没有按照这里提到的逗号分隔,randint

2 个答案:

答案 0 :(得分:2)

该页面上的输出显示解释器(或repr)输出。此外,我将其更改为randint并删除了会导致语法错误的句点。

import numpy as np
I = np.random.randint(0, 10, 10)
print(I) # => [9 4 2 7 6 3 4 5 6 2]
print(repr(I)) # => array([9, 4, 2, 7, 6, 3, 4, 5, 6, 2])
print(type(I)) # => <type 'numpy.ndarray'>
L = list(I)
print(L) # => [9, 4, 2, 7, 6, 3, 4, 5, 6, 2]

答案 1 :(得分:1)

将randomint更改为randint对我有用:

Z=np.arange(10)
I = np.random.randint(0,len(Z),20)
print I 

#[9 0 0 1 0 2 3 4 3 3 2 2 7 8 1 9 9 2 1 7]

#so this instruction works for me 
print Z[I]

# [3 9 6 6 7 7 7 3 7 5 5 2 1 1 5 7 1 0 7 4]