是否有一种可行的python方法来获取带forloops的高效输出

问题描述 投票:0回答:1

我需要打印在上面用for循环打印出来的那些在上部创建的值对,以便随着程序的增长,我可以动态地添加一些新的var ...

打印到cli应该看起来像

[vargroup] 
Varname:Varvalue
... 
[vargroup1] 
Varname:Varvalue
... 

我现在在这里坐了很长时间,尝试了不同的事情,但是在大多数情况下,它说明了一些有关整数和切片,元组的问题,并且forloop无法处理listindexes ...我对python真的很陌生,甚至对编程来说我也是不明白口译员要我什么...现在我的朋友告诉我,您可以帮助我:Oo

import configparser



def test():
#defining Arrays for Settingmanipulation
#default group



    default_rel = []
    default_rel.append('DEFAULT')

#program specific group
#define
    program_rel_name = []
    program_rel_val = []


    program_rel = [program_rel_name, program_rel_val]
    program_rel_name.append("")

#example for a vargroup
program_rel_val.append('OPERATIONAL')       #Name
    program_rel_name.append("FileName")
    program_rel_val.append('settings.ini')      #inifile Name
    program_rel_name.append("WorkDir")
    program_rel_val.append('..')                #workdir

#data related group
    data_rel_name = []
    data_rel_val = []

    data_rel = [data_rel_name, data_rel_val]
    data_rel_name.append("")
    data_rel_val.append ('DATA')  # Name
    data_rel_name.append("aIndex")
    data_rel_val.append ('sp500')  # aindex
    data_rel_name.append("dataSetType")
    data_rel_val.append ('jk')  # dataSet (joined Closes)
    data_rel_name.append("joinData")
    data_rel_val.append('True')         #mData (join those Closes)
    data_rel_name.append("visualizeData")
    data_rel_val.append('True')         #(v )run Visualizer

#maschine learning related
    ml_rel_name = []
    ml_rel_val = []

    ml_rel = [ml_rel_name, ml_rel_val]
    ml_rel_name.append("")
    ml_rel_val.append("MaschineLearning")   #Name
    ml_rel_name.append("runMlPreprocessor")
    ml_rel_val.append('True')               #run ml preprocessor


    inifile = [program_rel, data_rel, ml_rel]



#writing inifile
    config = configparser.ConfigParser ()
    cinfig ={}
    Group=1
    ValuePair=3
#output shall be to cli like demonstrated with this print
    print(inifile[Group][0][ValuePair] + ":"+ inifile[Group][1][ValuePair])
    print()
    print()

#by this forloop
    for count, Groups in enumerate(inifile):
        for counti, ValuePairs in inifile[count]:
            print(inifile[count][0][counti] + ": " + inifile[count][1][counti])



test()

在此代码上,我设计了一个for循环,以打印出存储在多维数组中的那些值对。...现在经过4个小时,尝试将所需的输出以param:value格式进行整理,以至于难以放弃。 。你有什么想法要完成吗?

由于在此类中创建了数组,因此给出了示例输入,输出是第69行中的错误,表示]

 for counti, ValuePairs in inifile[count][0]: ValueError: not enough values to unpack (expected 2, got 0) 
python for-loop command-line-interface
1个回答
0
投票

要打印inifile:

© www.soinside.com 2019 - 2024. All rights reserved.