【问题标题】:Trying to print from a 3D array by joining variable names and printing only one column尝试通过连接变量名称并仅打印一列来从 3D 数组打印
【发布时间】:2020-03-24 02:08:57
【问题描述】:
jEngines = [
            [["Aerotech 54mm Motor (J1799N)"], ["0.6"], ["1799"], ["1090"], ["540"]],
            [["Cesaroni 54mm Motor (J760)"], ["1.7"], ["760"], ["1076"], ["576"]],
            [["Aerotech 54mm Motor (J401FJ)"], ["2.8"], ["401"], ["912"], ["511"]],
            [["Aerotech 54mm Motor (J800T)"], ["1.6"], ["800"], ["1134"], ["595"]],
            [["Aerotech 38mm Motor (J825R)"], ["1.2"], ["825"], ["878"], ["497"]],
            [["Cesaroni 38mm Motor (J94)"], ["6.8"], ["94"], ["660"], ["372"]],
            [["Aerotech 38mm Motor (J425R)"], ["1.6"], ["425"], ["631"], ["364"]],
            [["Aerotech 38mm Motor (J500G)"], ["1.4"], ["500"], ["654"], ["375"]],
            [["Aerotech 38mm Motor (J420)"], ["1.6"], ["420"], ["635"], ["345"]],
            [["Aerotech 38mm Motor (J340M)"], ["1.8"], ["340"], ["577"], ["365"]]
            ]
kEngines = [
            [["Aerotech 54mm Motor (K456DM)"], ["2.9"], ["456"], ["1484"], ["866"]],
            [["Aerotech 54mm Motor (K2050ST)"], ["0.7"], ["2050"], ["2086"], ["1292"]],
            [["Cesaroni 54mm Motor (K300)"], ["8.4"], ["300"], ["2270"], ["1265"]],
            [["Cesaroni 54mm Motor (K260)"], ["8.7"], ["260"], ["2047"], ["1149"]],
            [["Cesaroni 54mm Motor (K1200)"], ["1.7"], ["1200"], ["1631"], ["960"]],
            [["Cesaroni 54mm Motor (K2045)"], ["0.7"], ["2045"], ["1290"], ["716"]],
            [["Cesaroni 54mm Motor (K940)"], ["1.8"], ["940"], ["1366"], ["768"]],
            [["Cesaroni 54mm Motor (K630)"], ["2.7"], ["630"], ["1410"], ["912"]]
            ]
def heightPrint():
    rEngine = input("Pick a class of engine a-l (lowercase):")
    rEngine= rEngine+("Engines")
    print("You have chosen class {}".format(rEngine))
    print("These are the engines in class {}".format(rEngine))
    for x in range 20:
        print(eval(rEngine[0][x]))

上面是 3D 数组的一部分,也是我要解决的问题。这可能是一种非常混乱的方式,但我希望能够在每个引擎之后使用 /n 从数组中打印引擎,但它不喜欢在 eval 行之后有方括号,也不喜欢在里面有一个 str方括号也是。

【问题讨论】:

  • 20 是作为一个例子给出的,因为我不确定如何让它达到数组中列的长度

标签: python loops multidimensional-array


【解决方案1】:

重要编辑:据我所知,使用 eval() 是不好的做法。如果您是唯一使用此工具的人,则以下解决方案很好,但如果其他人将访问此工具,则此方法很危险,因为输入 print(1)# 将使程序打印 1 并包含所有含义它。可以通过在接收到输入后检查输入字符是否在允许的输入范围内来修复代码

if rEngine not in ['k', 'j']:
    break # or do whatever action is needed, like prompting the user again

不过,解决此问题的“pythonic”方法是将名称映射到数组并以这种方式获取数组的字典

engines = {'jEngines': jEngines, 'kEngines': kEngines}
rEngine = engines[rEngine]

您可以在访问引擎之前eval() 引擎,然后将其作为数组访问

def heightPrint():
    rEngine = input("Pick a class of engine a-l (lowercase): ")
    rEngine = rEngine+"Engines"
    print("These are the engines in class {}".format(rEngine))
    print("You have chosen class {}".format(rEngine))
    rEngine = eval(rEngine)
    for x in rEngine:
        print(x[0][0])

我不确定你到底想打印什么,这个函数会给你命名为“Aerotech 54mm Motor (J1799N)”等等。

对于您帖子中的kEngines,该函数将产生以下输出:

heightPrint()

Pick a class of engine a-l (lowercase): k
You have chosen class kEngines
These are the engines in class kEngines
Aerotech 54mm Motor (K456DM)
Aerotech 54mm Motor (K2050ST)
Cesaroni 54mm Motor (K300)
Cesaroni 54mm Motor (K260)
Cesaroni 54mm Motor (K1200)
Cesaroni 54mm Motor (K2045)
Cesaroni 54mm Motor (K940)
Cesaroni 54mm Motor (K630)

【讨论】:

  • 所以我试图让它从阵列中为所选发动机类型打印发动机,例如:Aerotech 54mm 电机 (K456DM) Aerotech 54mm 电机 (K2050ST)、Cesaroni 54mm 电机 (K300)、切萨罗尼 54 毫米马达 (K260), 切萨罗尼 54 毫米马达 (K1200), 切萨罗尼 54 毫米马达 (K2045), 切萨罗尼 54 毫米马达 (K940), 切萨罗尼 54 毫米马达 (K630)
  • 例如: print(eval(rEngine[row[0] for row in rEngine)) 但它不喜欢这样。谢谢
  • 你试过我的代码 sn-p 了吗?打印引擎名称实际上就是它的作用;)我将编辑帖子以包含一个示例
  • 我之前尝试过它时出现错误,但后来设法让它工作,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2015-05-28
  • 2021-07-10
  • 1970-01-01
相关资源
最近更新 更多