This question already has an answer here:
这个问题在这里已有答案:
- Find all the occurrences of a character in a string 7 answers
- How to find all occurrences of a substring? 16 answers
在字符串中找到所有出现的字符7个答案
如何查找所有出现的子字符串? 16个答案
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]