2016-03-08 20 views
0
返回一个列表separte

我运行这段代码:numpy.random.randint不以逗号

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] 

返回一个列表,而不在elelements用逗号这里提到randint

+0

np.random.randomint(0,len(Z),20)。是bd语法。也许np.random.randint(0,len(Z),20)?? –

+0

@ DaveX,这是一个错字,我的意思是randint()我编辑了我的代码 – BetterEnglish

+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

更改不中隔离randomint to 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]