【发布时间】:2014-05-02 14:46:18
【问题描述】:
我正在尝试编写一个函数,它使用一个字符串和一个字符,并为该字符串中该字符的所有出现生成一个索引列表。
到目前为止,这就是我所拥有的,但它总是给我 []。
def list_of_indices(s,char):
string_lowercase = s.lower()
sorted_string = "".join(sorted(string_lowercase))
char_list = list(sorted_string)
for x in char_list:
a = []
if x == char:
a.append(char_list.index(x))
return a
我不明白为什么这不能产生答案。它必须是一个非空长度的列表。
有人知道如何获取所有事件的索引吗?
【问题讨论】:
标签: python string loops for-loop indices