【发布时间】:2016-06-03 10:57:46
【问题描述】:
我正在尝试编写一个函数,将所选行从列表复制到另一个,基于第三个列表应该采取的点。 “1” - 用这个东西复制行,“0”省略。 这是我的代码:
stuff = [
[1, 2, 3],
[10, 20, 30],
[100, 200, 300],
[1000, 2000, 3000],
[10000, 20000, 30000],
]
chooseThese = [1,0,1,1,0]
def fnChoose(arr1D, arr):
new = []
for digit in arr1D:
for row in arr:
if arr1D[digit] == 1:
new.append(arr[row])
else:
continue
return new
print (fnChoose(chooseThese, stuff))
结果我不想得到:
[[1, 2, 3], [100, 200, 300], [1000, 2000, 3000]]
不幸的是我的功能不起作用,空闲显示以下错误:
Traceback (most recent call last):
File "~\file.py", line 21, in <module>
fnChoose(chooseThese, stuff)
File "~\file.py", line 16, in fnChoose
new.append(arr[row])
TypeError: list indices must be integers or slices, not list
我应该如何纠正这个功能?如何将整行追加到列表中?
【问题讨论】: