如何在嵌套列表中搜索列表的编号并在其他嵌套列表中获取其索引?
How to search numbers of a list in a nested list and get its indexes in other nested list?
我声明:
anyNums = [
[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]
] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Rows to search in any numbers
我想做的是搜索 numsToSearch[0]
是否在 anyNums[rtsian[0]]
中(换句话说,我正在寻找第 0 行中编号 1 的索引anyNums
),如果它是 True
,则获取其在名为 indices
的其他嵌套列表中的一个或多个索引,如果它不是 true,则只需追加到嵌套列表 "The number is not in the list"
中,然后再次搜索 numsToSearch[1]
是否在 anyNums[rtsian[1]]
中,如果是 True
,则在嵌套列表 indices
中获取其索引。如果是False
,那么就在嵌套列表中追加"The number is not in the list"
.
对其他的重复此过程。所以在最后,当我打印 indices
这显示在控制台 [[0], [1,2], ["The number is not in the list"]]
.
我刚试过这个:
anyNums = [
[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]
] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Specially rows to search in any numbers
indices = []
for i in range(len(rtsian)):
for j in range(len(anyNums[i])):
if numsToSearch[i] in anyNums[rtsian[i]]:
indices[i].append(
anyNums[rtsian[i]].index(
anyNums[rtsian[i]][j]
)
)
else:
indices[i].append("The number is not in the list")
print(indices)
我得到下一个错误 IndexError: list index out of range
因为我知道我迷失了 for 循环和列表的正确索引。
希望有人能帮帮我,谢谢!
你的代码中有不少问题。一些主要的是
indixes[i].append()
:但是您只是创建了索引列表而从未创建索引[i] 子列表。要解决此问题,您可以添加 indixes.append([])
作为外部 for
循环中的第一行。
for j in range(len(anyNums[i]))
:我想在这里你想遍历 rtsian
提供的行,所以更好的是 for j in range(len(anyNums[rtsian[i]]))
以上两个正在生成IndexError
。
解决了以上两个问题后仍然得不到想要的输出,所以我对代码做了一些修改::
anyNums = [[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Specially rows to search in any numbers
indixes = []
for i in range(len(rtsian)):
indixes.append([])
found = False
for j in range(len(anyNums[rtsian[i]])):
if numsToSearch[i] == anyNums[rtsian[i]][j]:
indixes[i].append(j)
found = True
if not found:
indixes[i].append("The number is not in the list")
print(indixes)
输出:
[[0], [1, 2], ['The number is not in the list']]
注意:以上是 OP 的直观代码,尽管它可能不是解决他的查询的最优化代码。
我声明:
anyNums = [
[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]
] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Rows to search in any numbers
我想做的是搜索 numsToSearch[0]
是否在 anyNums[rtsian[0]]
中(换句话说,我正在寻找第 0 行中编号 1 的索引anyNums
),如果它是 True
,则获取其在名为 indices
的其他嵌套列表中的一个或多个索引,如果它不是 true,则只需追加到嵌套列表 "The number is not in the list"
中,然后再次搜索 numsToSearch[1]
是否在 anyNums[rtsian[1]]
中,如果是 True
,则在嵌套列表 indices
中获取其索引。如果是False
,那么就在嵌套列表中追加"The number is not in the list"
.
对其他的重复此过程。所以在最后,当我打印 indices
这显示在控制台 [[0], [1,2], ["The number is not in the list"]]
.
我刚试过这个:
anyNums = [
[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]
] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Specially rows to search in any numbers
indices = []
for i in range(len(rtsian)):
for j in range(len(anyNums[i])):
if numsToSearch[i] in anyNums[rtsian[i]]:
indices[i].append(
anyNums[rtsian[i]].index(
anyNums[rtsian[i]][j]
)
)
else:
indices[i].append("The number is not in the list")
print(indices)
我得到下一个错误 IndexError: list index out of range
因为我知道我迷失了 for 循环和列表的正确索引。
希望有人能帮帮我,谢谢!
你的代码中有不少问题。一些主要的是
indixes[i].append()
:但是您只是创建了索引列表而从未创建索引[i] 子列表。要解决此问题,您可以添加indixes.append([])
作为外部for
循环中的第一行。for j in range(len(anyNums[i]))
:我想在这里你想遍历rtsian
提供的行,所以更好的是for j in range(len(anyNums[rtsian[i]]))
以上两个正在生成IndexError
。
解决了以上两个问题后仍然得不到想要的输出,所以我对代码做了一些修改::
anyNums = [[1,8,4],
[3, 4, 5, 6],
[20, 47, 47],
[4, 5, 1]] #List for find the numbers
numsToSearch = [1, 47, 20] #Numbers to search
rtsian = [0, 2, 3] #Specially rows to search in any numbers
indixes = []
for i in range(len(rtsian)):
indixes.append([])
found = False
for j in range(len(anyNums[rtsian[i]])):
if numsToSearch[i] == anyNums[rtsian[i]][j]:
indixes[i].append(j)
found = True
if not found:
indixes[i].append("The number is not in the list")
print(indixes)
输出:
[[0], [1, 2], ['The number is not in the list']]
注意:以上是 OP 的直观代码,尽管它可能不是解决他的查询的最优化代码。