azalea says

Python:已知子字符串序列,返回子字符串在原字符串的所有位置(indices)

A.循环

myDNA = 'CAAGAGAAACGCAAAGAAACUGACAGAAGAGAGUGAGCACACAAAGGCAAUUU'
substr = 'UGACAGAAGAGAGUGAGCAC'
indices = [i for i in xrange(len(myDNA)) if myDNA.startswith(substr, i)] 
print indices

结果是

[20]

B.正则表达式

import re

myDNA = 'CAAGAGAAACGCAAAGAAACUGACAGAAGAGAGUGAGCACACAAAGGCAAUUU'
substr = 'UGACAGAAGAGAGUGAGCAC'
indices = [m.start() for m in re.finditer(re.compile(substr), myDNA)]
方法B的问题是如果2个子字符串有重叠,则不能找出第二个子字符串的位置
比如
myDNA = 'ATATATA'
substr = 'ATA'
方法A得到 indices=[0,2,4]
方法B得到 indices=[0,4]
所以如果子字符串有重叠时,只能用方法A
references
Search substring in a string and get index of all occurances.reply1 reply2
Find All Indices of a SubString in a Given String
programming python 字符串 正则表达式 · Tweet Edit