Python3 替代表达式的增量

Python3 alternative of increment for expressions

想象一下,我有 index = 0hash = '06123gfhtg75677687fgfg4',我想处理像 hash[i++] 这样的表达式。如何在 Python3 中做到这一点?

注意,我需要在这个表达式后加上 index = 1。如果可能的话,我需要一行表达式。

预期用法如下:

enc = bytes(enc % len(session_key))
x = bytes(data[i] ^ session_key[enc++]) + ej)
data[i] = ej = x

您可以使用 itertools.count() 对象,使用 next 给出当前值并递增它。这将正确模拟 ++ 后缀 C/C++ 运算符。

示例:

import itertools

c = itertools.count()  # starts at 0, but can be passed a start value as argument

s = 'abc'
print(s[next(c)])
print(s[next(c)])
print(s[next(c)])

按顺序打印 a,b,c。