在python的嵌套列表中查找元素的索引 [英] Finding the index of an element in nested lists in python
本文介绍了在python的嵌套列表中查找元素的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取python嵌套列表中元素的索引-例如[[a, b, c], [d, e, f], [g,h]]
(并非所有列表的大小均相同).
我尝试使用
I am trying to get the index of an element in nested lists in python - for example [[a, b, c], [d, e, f], [g,h]]
(not all lists are the same size).
I have tried using
strand_value= [x[0] for x in np.where(min_value_of_non_empty_strands=="a")]
但这仅返回一个空列表,即使存在该元素也是如此.知道我在做什么错吗?
but this is only returning an empty list, even though the element is present. Any idea what I'm doing wrong?
推荐答案
def find_in_list_of_list(mylist, char):
for sub_list in mylist:
if char in sub_list:
return (mylist.index(sub_list), sub_list.index(char))
raise ValueError("'{char}' is not in list".format(char = char))
example_list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
find_in_list_of_list(example_list, 'b')
(0, 1)
这篇关于在python的嵌套列表中查找元素的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文