python - 有什么方法可以在一步 for 循环中执行两步吗?

标签 python python-3.x

我正在使用 Python 3.8,我想知道是否有任何方法可以在一步 for 循环中执行两个步骤,如下所示:

for i in range(15):
 if i == 2:
  # make two steps
 else:
  #continue normaly

最佳答案

手动操作发电机

gen = iter(range(15)) 
while True:
    try:
        i = next(gen) 
        if i ==2:
            next(gen) 
            continue 
        else:
            pass
    except: StopIteration
        break 

关于python - 有什么方法可以在一步 for 循环中执行两步吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60015478/

相关文章:

python - Keras 嵌入, "weights"参数在哪里?

python - 通过搜索unix中的所有目录来编译值列表

python - 最小化简单的线性组合

python - 使用 celery 使用 AMQP 中的 json 数据

Python 优雅地在 int() 调用上失败?

python - 是否有计算部分和的 NumPy 函数?

python - 在 Ubuntu 上安装 Python Nmap 的问题

python - 如何在python中将二维numpy方形数组旋转45度?

python - Twitch IRC 聊天机器人成功连接但未检测到命令

python - 从给定字符串中提取最大数值