【问题标题】:list of dict, how to fill out information laterdict列表,以后如何填写信息
【发布时间】:2014-07-12 05:07:19
【问题描述】:

我有一个这样的字典列表:

mylist=[
    {
        'name':None,
        'Id':sys.argv[1]
    },{
        'name':None,
        'Id':sys.argv[2]
    },{
        'name':None,
        'Id':sys.argv[3]
    }
]

我稍后调用一个子流程并处理它的输出,我想将输出放在“名称”值字段中。在我调用命令后,我得到了所有行的列表,我读了这样的行

for line in content:
    if line.startswith('some_identifier'):
        line.strip('\n')
        #put the line into an unused 'name' value field

稍后我想生成一个由操作系统运行的登录命令,如下所示:

for info in mylist
    subprocess.check_output(['iscsicli.exe', 'LoginTarget', info['name'], 'T', portalip, portalport, '*', info['Id'], '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'])

所以我想要做的是能够将第二个代码 sn-p 中读取的行放入 mylist 中未使用的“名称”插槽中

【问题讨论】:

  • 你什么时候用过myList
  • 我稍后根据列表中的信息生成一些信息。我对其进行迭代并生成内容。
  • 但是您甚至以什么方式将现有结构与代码联系起来?
  • 结构没那么重要,什么ID关联什么名字不使用t matter to me but it is important that the 3 different ids。它用于 iSCSI 目标的登录脚本。我正在更新我的问题,以便更清楚
  • 我在这里看到了一些说法,但问题是什么?

标签: python list dictionary iterator


【解决方案1】:
for line in content:
    if line.startswith('some_identifier'):
        line.strip('\n')
        #put the line into an unused 'name' value field
        for dict in mylist:
            if dict['name'] == None:
                dict['name'] = line
                break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多