Python: 将选定的行从列表复制到另一个
Python: copying chosen rows form list to another
我正在尝试编写一个函数,将选定的行从一个列表复制到另一个,基于第三个列表,该列表指出应该采用的点。 “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
我应该如何修正这个功能?如何将整行附加到列表?
一种更简单的方法是使用 索引列表 而不是 0/1 列表,以使用 enumerate
创建 索引理解 或使用 zip
的组合列表:
stuff = [
[1, 2, 3],
[10, 20, 30],
[100, 200, 300],
[1000, 2000, 3000],
[10000, 20000, 30000],
]
chooseThese = [1,0,1,1,0]
# use enumerate (two variants)
new = [s for index, s in enumerate(stuff) if chooseThese[index] == 1]
print(new)
new = [stuff[index] for index, choose in enumerate(chooseThese) if choose == 1]
print(new)
# use zip
new = [s for choose, s in zip(chooseThese, stuff) if choose == 1]
print(new)
# use indexed chooser-variable
chooseThese = [0,2,3]
new = [stuff[index] for index in chooseThese]
print(new)
您应该跟踪您要追加的列表的索引。
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 = []
index = 0
for row in arr:
if (arr1D[index] == 1):
new.append(row)
index += 1
return new
print (fnChoose(chooseThese, stuff))
我正在尝试编写一个函数,将选定的行从一个列表复制到另一个,基于第三个列表,该列表指出应该采用的点。 “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
我应该如何修正这个功能?如何将整行附加到列表?
一种更简单的方法是使用 索引列表 而不是 0/1 列表,以使用 enumerate
创建 索引理解 或使用 zip
的组合列表:
stuff = [
[1, 2, 3],
[10, 20, 30],
[100, 200, 300],
[1000, 2000, 3000],
[10000, 20000, 30000],
]
chooseThese = [1,0,1,1,0]
# use enumerate (two variants)
new = [s for index, s in enumerate(stuff) if chooseThese[index] == 1]
print(new)
new = [stuff[index] for index, choose in enumerate(chooseThese) if choose == 1]
print(new)
# use zip
new = [s for choose, s in zip(chooseThese, stuff) if choose == 1]
print(new)
# use indexed chooser-variable
chooseThese = [0,2,3]
new = [stuff[index] for index in chooseThese]
print(new)
您应该跟踪您要追加的列表的索引。
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 = []
index = 0
for row in arr:
if (arr1D[index] == 1):
new.append(row)
index += 1
return new
print (fnChoose(chooseThese, stuff))