https://blog.csdn.net/jasminefeng/article/details/115794017
filter过滤器
没有循环,总共只要三步,当然如果你不觉得晦涩难懂的话,可以强行写成一步~
>>>s_idx = list(zip(range(len(s)),s)) # 生成下标
>>>s_idx
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'a'), (4, 'b'), (5, 'c'), (6, 'a'), (7, 'b'), (8, 'c')]
>>>res_idx = list(filter(lambda x:x[1]==pat,s_idx)) # 过滤掉不满足条件的元素
>>>res_idx
[(1, 'b'), (4, 'b'), (7, 'b')]
>>>res = list(zip(*res_idx))[0] # 反用zip,重新组合生成下标序列
>>>res
(1,4,7)