Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回python列表中元素所在的所有索引_Python - Fatal编程技术网

返回python列表中元素所在的所有索引

返回python列表中元素所在的所有索引,python,Python,这只返回列表中字符所在的第一个索引值。我需要存在char的所有索引 def find_loc(char): for sub_list in chunks: if char in sub_list: return chunks.index(sub_list), sub_list.index(char) Index方法返回遇到的第一个索引值 有关方法\u描述符的帮助: index(...) L.index(value, [star

这只返回列表中字符所在的第一个索引值。我需要存在char的所有索引

  def find_loc(char):
    for sub_list in chunks:
        if char in sub_list:
                return chunks.index(sub_list), sub_list.index(char)

Index方法返回遇到的第一个索引值

有关方法\u描述符的帮助:

index(...)
    L.index(value, [start, [stop]]) -> integer -- return first index of value.
    Raises ValueError if the value is not present.
使用以下代码段:

def indexes(iterable, obj):
    encounters = []
    for i in range(len(iterable)):
        if iterable[i] == obj:
            encounters.append(i)
    return encounters

标志着“质量很低”。看看是否有任何方法可以获得所有索引值,使用并设置一个条件。@shivamsinghal检查上面的枚举链接。