答案 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