Python 将列表中的元素指定给数组
这是我的节目Python 将列表中的元素指定给数组,python,arrays,list,Python,Arrays,List,这是我的节目 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 我成功地将给定的sting转换为列表,但我正在
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
我成功地将给定的sting转换为列表,但我正在尝试将horas[0]中的对象分配给数组[0][0],将horas[1]分配给数组[0][1]等等。它不断向我显示“列表索引超出范围”错误消息。help因此外部“for”中的i+1使索引超出范围抱歉,我的坏lol如果要使用函数l2a(列表到数组位置),该函数接收列表位置和数组的维度,并返回该列表位置的x和y,您可以编写:
for pos, value in enumerate(horas):
x, y = l2a(pos, 2, 2)
array[x][y] = value
现在只需编写l2a,就可以设置任何数组大小。(提示,请使用
/
和%
运算符。)遇到此错误时,您认为i
的值是什么?如果您不知道,请在循环期间打印i并查看失败的位置Hanks brian,解决了它!!c: