Python - 如何在字符串中查找“重复”字符? [重复]

时间:2022-12-03 21:40:29

This question already has an answer here:

这个问题在这里已有答案:

For example in 'GATTACA' I want to find all the 'A's.

例如在'GATTACA'中我想找到所有'A'。

I would want the positions 1,4,6.

我希望持仓1,4,6。

Using the '.find' method only gives me the location of the first 'A' but not the rest.

使用'.find'方法只能给我第一个'A'的位置,而不是其余的。

I was wondering if there was another method that allowed you to find all 'repeat' characters in a string?

我想知道是否有另一种方法可以让你在字符串中找到所有'重复'字符?

1 个解决方案

#1


In [26]: s = 'GATTACA'

In [27]: [i for i,char in enumerate(s) if char=="A"]
Out[27]: [1, 4, 6]

#1


In [26]: s = 'GATTACA'

In [27]: [i for i,char in enumerate(s) if char=="A"]
Out[27]: [1, 4, 6]