首页 > 解决方案 > Python3 表达式增量的替代方案

问题描述

想象一下,我有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

标签: pythonpython-3.5increment

解决方案


您可以使用itertools.count()object, withnext来给出当前值并增加它。这将++正确模拟后缀 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。


推荐阅读