如何简化通过索引搜索列表项?
how do I simplify searching for a list item by an index?
我有一个简单的程序:
pos = [1,2]
searched = [
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
]
print(searched[pos[0]][pos[1]])
7
现在我想要的是一些摆脱 searched[pos[0]][pos[1]]
的方法,只需输入类似 searched[[pos]]
.
的内容
有什么办法吗,还是每次都要写出来
I have gotten a lot of suggestions, but what I am searching for is a way to do this in one neat line, simplifying everything.
That means that things like using a function, converting to a specific dictionary or even enumerate don't work for me, so for anyone looking at this post later:
.
I suggest using np.array(variable)
while defining said variable so you can use variable[pos]
不知道你是否可以接受,但是一个函数就可以了:
def search_for_list_item_by_index(a_list, row, col):
return a_list[row][col]
print(search_for_list_item_by_index(searched, 1, 2))
这会按预期打印 7
。
您可以将其转换为 numpy 数组并进行搜索:
import numpy as np
pos = [1,2]
searched = np.array([
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
])
print(searched[1,2])
# 7
您可以按照@oppressionslayer 的建议将数组转换为 numpy。
另一种方法是创建字典并按如下方式使用它:
pos = [1,2]
searched = [
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
]
m=4 # width of the searched array
n=4 # hight of the searched array
searched = {(i,j):searched[i][j] for j in range(m) for i in range(n)}
print(searched[1,2]) # prints 7
print(searched[tuple(pos)]) # prints 7
希望对您有所帮助!!
您可以使用以下功能:
def func(idx, lst):
for i in idx:
lst = lst[i]
return lst
func(pos, searched)
# 7
我会使用字典方法 但有趣的是您可以使用 enumerate
:
泛化到非矩形列表
searched = {(row,col):item
for row,elems in enumerate(searched)
for col,item in enumerate(elems)}
或使用 做同样的事情,但适用于完全任意的嵌套,包括字典。可能不是你想要的方向但值得一提。
data = flatten_data(searched)
pos = [1,2]
print(data[1,2])
print(data[tuple(pos)])
我有一个简单的程序:
pos = [1,2]
searched = [
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
]
print(searched[pos[0]][pos[1]])
7
现在我想要的是一些摆脱 searched[pos[0]][pos[1]]
的方法,只需输入类似 searched[[pos]]
.
有什么办法吗,还是每次都要写出来
I have gotten a lot of suggestions, but what I am searching for is a way to do this in one neat line, simplifying everything.
That means that things like using a function, converting to a specific dictionary or even enumerate don't work for me, so for anyone looking at this post later:
.
I suggest usingnp.array(variable)
while defining said variable so you can usevariable[pos]
不知道你是否可以接受,但是一个函数就可以了:
def search_for_list_item_by_index(a_list, row, col):
return a_list[row][col]
print(search_for_list_item_by_index(searched, 1, 2))
这会按预期打印 7
。
您可以将其转换为 numpy 数组并进行搜索:
import numpy as np
pos = [1,2]
searched = np.array([
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
])
print(searched[1,2])
# 7
您可以按照@oppressionslayer 的建议将数组转换为 numpy。 另一种方法是创建字典并按如下方式使用它:
pos = [1,2]
searched = [
[1,3,4,6],
[2,6,7,8],
[0,1,2,8],
[5,6,9,2]
]
m=4 # width of the searched array
n=4 # hight of the searched array
searched = {(i,j):searched[i][j] for j in range(m) for i in range(n)}
print(searched[1,2]) # prints 7
print(searched[tuple(pos)]) # prints 7
希望对您有所帮助!!
您可以使用以下功能:
def func(idx, lst):
for i in idx:
lst = lst[i]
return lst
func(pos, searched)
# 7
我会使用字典方法 enumerate
:
searched = {(row,col):item
for row,elems in enumerate(searched)
for col,item in enumerate(elems)}
或使用
data = flatten_data(searched)
pos = [1,2]
print(data[1,2])
print(data[tuple(pos)])