Reputation: 13
Hello every one I have a question about checking string, I want to check the specific characters in the word by orders
Ah = "a","h","i"
Oo= "o", "y","wh"
text = "whats going on my friend?"
text_splited = text.split()
for word in text_splited:
print "Checking this :",word
for ph in Ah:
if ph in word:
print ph
for ph in Oo:
if ph in word:
print ph
The result is com out like this :
Checking this : whats
a
h
wh
Checking this : going
i
o
Checking this : on
o
Checking this : my
y
Checking this : friend?
Lip : i
for example "whats" the expected result is the "wh","h","a" (in order) anyone can help, please :)
Thanks
Upvotes: 0
Views: 61
Reputation: 7206
If you don't care the mixing of the elements of Oo
and Ah
. This might works:
Ah = "a","h","i"
Oo= "o", "y","wh"
text = "whats going on my friend?"
text_splited = text.split()
for word in text_splited:
list1 = []
list2 = []
print("Checking this :",word)
for ph in Ah:
list1.append((word.find(ph),ph)) #to preserve the index of element
list2.extend(list1)
for ph in Oo:
list2.append((word.find(ph),ph)) #to preserve the index of element
list2.sort(key=lambda x:x[0])
for i in list2:
if(i[0]>=0):
print(i[1])
we are just printing the found elements in sorted order. Output of above code is:
Checking this : whats
wh
h
a
Checking this : going
o
i
Checking this : on
o
Checking this : my
y
Checking this : friend?
i
Upvotes: 1