> Den 05/01/2015 kl. 22.45 skrev Dariusz Mysior <
mysior...@gmail.com>:
>
> I try develop this code to get a number of position this letter in word, and I have it but for example if i search letter "t" I get "1, 5, b", why I get b instead of 11???
>
> def gen2():
> count=0
> count2=0
> for a in zmienna:
> count2+=1
> if a==szukana:
> count+=1
> pozycja.append(count2)
>
> return pozycja
> print("3. Literka '",szukana,"' w słowie ",zmienna,
> "wystąpiła " ,gen(),"razy w kolejności")
> #print(gen2())
>
> print(", ".join(["%x" % a for a in gen2()]))
Whoa, that code is really convoluted. It won't even compile, as far as I can see. Try this instead:
import re
def indices(needle, haystack):
return [match.start() for match in re.finditer(re.escape(needle), haystack)]
If your search strings can be overlapping, you'll need another solution.
Erik