我对 Python 很陌生,但我遇到了一个 Google 尚未为我解决的问题。我有一个字符串列表(f_list)。我想生成包含特定字符('>')的字符串的索引列表。
示例:f_list = ['>EntryA', EntryB, '>EntryC', EntryD]
我想生成: index_list = [0, 2]
此代码有效,但我必须为值输入字符串的确切名称(即>EntryA)。如果我输入“>”(如下面的代码示例所示),它在 index_list 中不返回任何值。
f_list = ['>EntryA', 'EntryB', '>EntryC', 'EntryD']
index_list = []
def all_indices(v, qlist):
idx = -1
while True:
try:
idx = qlist.find(v, idx+1)
index_list.append(idx)
except ValueError:
break
return index_list
all_indices('>', f_list)
print(index_list)