简体   繁体   中英

how to find previous character and index for character

您能帮我这个忙吗,所以我想要的是,我输入文本“ hello”,我得到几秒钟一一索引的字符

For variant 2, instead of:

if lastElement == (ch - 1):

what about:

if lastElement == text[elmt-1]

This produces the desired output:

target = 'ol'
after = 'l'
before = 'e'

text = 'hello'

for elmt, ch in enumerate(text):
    print(ch, elmt)
    if ch in target:
        print('was {}'.format(ch.upper()))
    if ch == after:
        if last in before:
            print('before this {} was {}\n'.format(ch.upper(), last.upper()))
        else:
            print('before was other character not {}\n'.format(before.upper()))
    last = ch

Output:

h 0
e 1
l 2
was L
before this L was E

l 3
was L
before was other character not E

o 4
was O

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