这只返回列表中存在char的第一个索引值。我需要所有字符存在的指数。返回元素出现在python列表中的所有索引
def find_loc(char):
for sub_list in chunks:
if char in sub_list:
return chunks.index(sub_list), sub_list.index(char)
这只返回列表中存在char的第一个索引值。我需要所有字符存在的指数。返回元素出现在python列表中的所有索引
def find_loc(char):
for sub_list in chunks:
if char in sub_list:
return chunks.index(sub_list), sub_list.index(char)
索引方法返回遇到的第一个索引值。
帮助上method_descriptor:
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
检举为 “质量很低”。看看[“我该如何提出一个好问题?”](https://stackoverflow.com/help/how-to-ask) –
[如何查找列表中所有元素的出现? ](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) –