简体   繁体   English

Python - 使用 For 循环打印 arrays

[英]Python - Printing arrays using For Loop

I have a multiple 2D arrays with name temp1, temp2, temp3... etc我有多个 2D arrays 名称为 temp1、temp2、temp3... 等

temp1 = [[7, 2, 4],
         [5, 0, 6],
         [8, 3, 1]] 

temp2 = [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]] 

temp3 = [[2, 2, 2],
         [2, 2, 2],
         [2, 2, 2]] 

I want to run a for loop that will return only some of the arrays depending on the range of i .我想运行一个for 循环,根据i 的范围仅返回一些 arrays 。 I am thinking of something like我在想类似的东西

for i in range(1,3):
    arrayName = ("temp" + str(i))
    print(arrayName)

where print(arrayName) prints out the actual array instead of a string其中print(arrayName)打印出实际的数组而不是字符串

Any help is appreciated!任何帮助表示赞赏!

You could do this with exec() .你可以用exec()做到这一点。 Also your range() should have an upper bound of your max value plus one.此外,您的range()应该具有最大值加一的上限。 The upper bound is not inclusive.上限不包括在内。

for i in range(1, 4):
    exec(f'print(temp{str(i)})')

Output: Output:

[[7, 2, 4], [5, 0, 6], [8, 3, 1]]
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
[[2, 2, 2], [2, 2, 2], [2, 2, 2]]

You can use eval() .您可以使用eval() It allows you to get a variable by name.它允许您按名称获取变量。

for i in range(1, 4):
    current_array = eval(f'temp{i}')
    print(current_array)

But, I advise you to set this part of code to the try/except block because you can have NameError exception.但是,我建议您将这部分代码设置为 try/except 块,因为您可能会遇到 NameError 异常。 And your code will look like:您的代码将如下所示:

for i in range(1, 4):
    variable_name = f"temp{i}"

    try:
        current_array = eval(variable_name)
        print(current_array)
    except NameError:
        print(f"Can not get variable with {variable_name} name")

I notice that this is your requirement:我注意到这是您的要求:

... where print(arrayName) prints out the actual array instead of a string ...其中 print(arrayName) 打印出实际的数组而不是字符串

If so, you could use the following simplified design pattern:如果是这样,您可以使用以下简化的设计模式:

arrays = [array1, array2, array3]
for array in arrays: 
    print(array)

And, in modification of your code, so that print() will 'print out the actual array instead of a string':并且,在修改代码时, print() 将“打印出实际的数组而不是字符串”:

temp1 = [[7, 2, 4],
         [5, 0, 6],
         [8, 3, 1]]

temp2 = [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]]

temp3 = [[2, 2, 2],
         [2, 2, 2],
         [2, 2, 2]]

temps = [temp1, temp2, temp3]

for i in temps:
    print(i)

Some further thoughts:一些进一步的想法:

  1. I would avoid the eval() or exec() Python methods as suggested by the other commenters.我会避免其他评论者建议的 eval() 或 exec() Python 方法。 Simpler solutions are possible;更简单的解决方案是可能的; you do not have a concrete reason to use dynamic execution.您没有使用动态执行的具体理由。

  2. There is a cleaner way to refactor your code, but I am providing the above answer that mirrors your code structure more directly so as to avoid confusion.有一种更简洁的方法来重构您的代码,但我提供的上述答案更直接地反映了您的代码结构,以避免混淆。

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

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