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]
しかし、私はこれを行うための最良の方法が欲しい