简体   繁体   English

Python3 表达式增量的替代方法

[英]Python3 alternative of increment for expressions

Imagine, I have index = 0 , hash = '06123gfhtg75677687fgfg4' and I want to process expression like hash[i++] .想象一下,我有index = 0hash = '06123gfhtg75677687fgfg4'并且我想处理像hash[i++]这样的表达式。 How to do this in Python3 ?如何在 Python3 中做到这一点?

Notice, I need index = 1 after this expression.注意,我需要在这个表达式后加上index = 1 I need one-line expression, if possible.如果可能,我需要单行表达式。

Expected usage below:预期用途如下:

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

You could use itertools.count() object, with next to give the current value and increment it.您可以使用itertools.count()对象,使用next给出当前值并增加它。 That would emulate ++ postfix C/C++ operator properly.这将正确模拟++后缀 C/C++ 运算符。

Example:例子:

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)])

prints a,b,c in sequence.按顺序打印 a,b,c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM