Skip to content

List of index of occurance of words in string but with reverse indexing #8169

Closed Answered by sourabhkv
Rinx1er asked this question in Q&A
Discussion options

You must be logged in to vote
string="hello how are you i am programming in python , i am also using github"
key="h"#can be single or multi
occurance=[]#this is +ve index

for i in range(len(string)-len(key)+1):
    if string[i:i+len(key)]==key:
        occurance.append(i)

#print(occurance)

occurance_rev = [i-len(string) for i in occurance]
print(occurance_rev)

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@sourabhkv
Comment options

@Rinx1er
Comment options

@sourabhkv
Comment options

Answer selected by Rinx1er
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants