Skip to main content
 首页 » 编程设计

python之将列表中的元素分配给数组

2024年10月01日8zhwl

这是我的程序

horas= str(raw_input()) 
horas=horas.split(' ',horas.count(' ')) 
print horas 
array=[ 
    [0,0], 
    [0,0] 
] 
i=0 
for row in range(2): 
    for column in range (2): 
        array[row][column]=horas[i] 
        i+=1 
    i+=1 

我成功地将给定的字符串转换为列表,但我试图将 horas[0] 中的对象分配给数组[0][0],将 horas[1] 分配给数组[0][1],等等。它一直向我显示“列表索引超出范围”的错误消息。帮助

请您参考如下方法:

所以外部 'for' 中的 i+1 使索引超出范围对不起我的坏笑