简体   繁体   中英

Find all index values for string t in string s?

Input:

s = "ADOBECODEBANC"
t = "ABC"  

Output:

{A:[0,10] , B:[3.9], C:[5,12]}

Do we have any in built function ?

There's no builtin function, but you can use enumerate() for the task:

s = "ADOBECODEBANC"
t = "ABC"

out = {}
for i, ch in enumerate(s):
    if ch in t:
        out.setdefault(ch, []).append(i)

print(out)

Prints:

{'A': [0, 10], 'B': [3, 9], 'C': [5, 12]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM