【问题标题】:Write a line with for-loop? [closed]用for循环写一行? [关闭]
【发布时间】:2020-12-02 17:24:08
【问题描述】:

我想完全像这样定义变量xx

xx = ('xx_1','xx_2','xx_3','xx_4','xx_5')

使用 python 使用 for 循环。谁能帮我解决这个问题?

【问题讨论】:

  • 欢迎堆栈溢出!不幸的是,这不是一个代码编写或教程网站,我们要求问题包括一个minimal reproducible example,其中包括 code,以了解您根据自己的研究已经尝试过的内容,以及出了什么问题您的尝试

标签: python for-loop variables


【解决方案1】:

xx = tuple('xx_' + str(i) for i in range(1, 6))

您正在寻找的文档在这里: https://www.python.org/dev/peps/pep-0289/

【讨论】:

    【解决方案2】:

    开个玩笑,

    xx = tuple()
    for x in ('xx_1', 'xx_2', 'xx_3', 'xx_4', 'xx_5'):
        xx += (x, )
    

    @Mark 的答案就是您要找的。下次请发布您的尝试,以便我们提供帮助,而不仅仅是为您编写解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多