如何找到字符的前一个字符和索引

时间:2016-03-12 19:14:27

标签: python

你可以帮我解决这个问题 所以我想要的是,我输入文字"你好"我在几秒钟内逐个获得带索引的字符

2 个答案:

答案 0 :(得分:1)

对于变体2,而不是:

if lastElement == (ch - 1):

怎么样:

if lastElement == text[elmt-1]

答案 1 :(得分:0)

这会产生所需的输出:

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

输出:

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