获取python字符串中字符集的位置
字符集:
string="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
charPositionToFind=A,D,V,Y
预期输出
postions=[0,3,21,24]
我这样做
def find_all(string,char):
return [i - 1 for i in range(len(string)) if string.startswith(char, i - 1)]
string="ABCDEYYFGHIAAJKVLMNOPDCQRSTAAVVVUVWXYZ"
charPositionToFind=['A','D','V','Y']
position=[]
for char in charPositionToFind:
s = find_all(string,char)
position.extend(s)
print sorted(position)
output:
[0, 3, 5, 6, 11, 12, 15, 21, 27, 28, 29, 30, 31, 33, 36]
但我想要最好的方法来做到这一点