【问题标题】:Python3 alternative of increment for expressionsPython3 表达式增量的替代方案
【发布时间】:2018-11-01 21:16:15
【问题描述】:

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

【问题讨论】:

  • hash[i] 然后在下一行i += 1
  • @Aran-Fey 我需要单行表达,如果可能的话
  • “如果可能的话,我需要单行表达式”——你认为你为什么需要这个?
  • enc++ 应该做什么?
  • 您的示例不需要在x = 行内隐藏更新。您可以在下一行执行enc += 1,您将获得更清晰、更易于理解的代码。

标签: python python-3.5 increment


【解决方案1】:

您可以使用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。

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2013-12-07
    相关资源
    最近更新 更多