简体   繁体   English

如何从多维列表中的元素获取索引?

[英]How can I get index from an element in a mulitidimensional list?

Here is what I'm trying to do:这是我正在尝试做的事情:

board = [[0, 1, 2, 3] , [4, 5, 6, 7] , [8, 9, 10, 11] , [12, 13, 14, 15,]].index("8")
print(board)

I get the error:我得到错误:

ValueError '8' is not in the list

How can I get the index from this element?如何从该元素中获取索引?

You can iterate and return the index value:您可以迭代并返回索引值:

next((i for i, x in enumerate(board) if n in x), 'not found')

board = [[ 0 , 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] , [ 8 , 9 , 10 , 11 ] , [ 12 , 13 , 14 , 15 , ]]

n = 8
print(next((i for i, x in enumerate(board) if n in x), 'not found'))

This prints:这打印:

2

In case you want all indices of the value, switch to a list comprehension:如果您想要该值的所有索引,请切换到列表理解:

[i for i, x in enumerate(board) if n in x]

I don't think there is a built in way to do it.我认为没有内置的方法可以做到这一点。 I would do it using numpy:我会使用 numpy 来做到这一点:

import numpy as np
board = [[ 0 , 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] , [ 8 , 9 , 10 , 11 ] , [ 
12 , 13 , 14 , 15 , ]]
b=np.asarray(board)
print(np.argwhere(b==8))

result: array([[2, 0]])结果: array([[2, 0]])

this assumes there is only one occurrence of an element.这假设一个元素只出现一次。

board = [[ 0 , 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] , [ 8 , 9 , 10 , 11 ] , [ 12 , 13 , 14 , 15 ]]

var = [ele for ele in board if 8 in ele][0]

# check if element is not present in the list
if len(var) == 0:
    print('-1')

else:
    print('index: (', board.index(var), var.index(8), ')')

output: output:

index: ( 2 0 )

What index in python does is find the element match which in your case are all lists and you are trying to match a int python 中的index是找到在您的情况下都是列表的元素匹配,并且您正在尝试匹配一个int

You can use numpy to get the location您可以使用numpy来获取位置

import numpy as np
board = [[ 0 , 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 ] , [ 8 , 9 , 10 , 11 ] , [ 12 , 13 , 14 , 15 ]]
np.where(np.array(board) == 8)
(array([2]), array([0]))

which is basically board[2][0]这基本上是board[2][0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在列表列表中获取外部列表元素的索引? - How can I get an index of an outer list element in a list of lists? 如何从从该矩阵生成的列表中获取矩阵中元素的索引? - How can I get the index of an element in a matrix from a list that is generated from that matrix? 如何获取列表中最大值的索引,然后使用最大值的索引从另一个列表中打印值? - How can I get the index of max values in list and then print the values from another list with max's index? 如何获取里面有列表的python numpy数组中任何元素的索引? - How can I get Index of any element in python numpy array that has list inside? 不知道索引号时如何从列表中删除元素 - How can I delete an element from a list when I do not know it's index number 如何从两个单独的列表中获取公共 integer 元素的索引并将其插入另一个列表? - How do I get the index of the common integer element from two separate lists and plug it to another list? 如何从 Python 的 Kivy 库中获取 Spinner 中特定元素的索引? - How can I get the index of a specific element in a Spinner from Python's Kivy library? 如何从列表中删除此元素? - How can I remove this element from the list? 如何在 python 中的 matplotlib 图中的 hover 上的给定列表中显示元素的索引号? - How can I show element's index number from a given list on hover in matplotlib graph in python? 如何获取List元素的引用? - How can I get the reference of a List element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM