通过连接变量名并只打印一列,尝试从三维数组打印

2024-09-29 00:20:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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]))

上面是三维阵列的一部分,我正试图解决这个问题。这可能是一个非常混乱的方法,但我想能够打印引擎从阵列与/n后,每一个,但它不喜欢有方括号后的求值行,也不喜欢有一个str在方括号里


Tags: informatclassprintmotor方括号j401fjj1799n
1条回答
网友
1楼 · 发布于 2024-09-29 00:20:54

重要编辑:如我所知,使用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])

我不知道你到底想打印什么,这个功能将得到你的名字“航空科技54mm电机(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)

相关问题 更多 >