我有一个简单的程序:
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]
您可以按照@suppressionslayer的建议将数组转换为numpy。 另一种方法是创建字典并按如下方式使用:
希望这有帮助!!你知道吗
您可以将其转换为numpy数组并获取您要查找的搜索结果:
不知道您是否接受,但函数可以:
这将按预期打印
7
。你知道吗相关问题 更多 >
编程相关推荐