在python中的嵌套列表中查找元素的索引

bio*_*hem 2 python numpy nested-lists

我试图在 python 中的嵌套列表中获取元素的索引 - 例如[[a, b, c], [d, e, f], [g,h]](并非所有列表的大小都相同)。我试过使用

strand_value= [x[0] for x in np.where(min_value_of_non_empty_strands=="a")]
Run Code Online (Sandbox Code Playgroud)

但这只是返回一个空列表,即使该元素存在。知道我做错了什么吗?

Dai*_*arf 6

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)
Run Code Online (Sandbox Code Playgroud)