简体   繁体   中英

Check specific characters in String by order

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

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

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