Python目录-多个实例或“子目录”问题-以编程方式添加项目

时间:2019-05-10 01:52:43

标签: python python-3.x loops dictionary

我想在python中创建一个字典,该字典具有许多需要以编程方式填充的“子值”。该字典将用于将文档添加到MongoDB数据库。

我最终想要使用的字典的想法是:

    host_dict = {
    'installed_applications':
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }
        {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }
}

我想做的是:

host_dict = {}
apps = get_installed_apps(host)
host_dict['installed_applications'] = {}
for app in apps:
    host_dict['installed_applications']['name'] = app[0]
    host_dict['installed_applications']['version'] = app[1]
    host_dict['installed_applications']['uninstall_string'] = app[2]
    host_dict['installed_applications']['install_date'] = app[3]
    host_dict['installed_applications']['install_location'] = app[4]
    host_dict['installed_applications']['publisher'] = app[5]

问题在于,它不会追加只是保持覆盖一个“ sub-dict”(就是您所称呼的那个?)的应用程序的每个实例

2 个答案:

答案 0 :(得分:0)

您的host_dict示例不是有效的python结构,但是您可以通过将installed_applications值作为列表来解决此问题,因此可以将项目追加到此列表中,每个项目都是字典,像这样:

apps = get_installed_apps(host)
host_dict = {'installed_applications': []}
for app in apps:
    new_app = {
        'name': app[0],
        'version': app[1],
        'uninstall_string': app[2],
        'install_date': app[3],
        'install_location': app[4],
        'publisher': app[5]
    }
    host_dict['installed_applications'].append(new_app)

最后host_dict['installed_applications']将有一个列表,其中每个值都是 app 字典,类似于此:

host_dict = {
'installed_applications': [
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }]
}

答案 1 :(得分:0)

如果我错了,请纠正我,但是host_dict不是有效的字典,我将假设您尝试创建一个键为installed_applications并且值作为列表的字典,因此它看起来像这样

host_dict = {
    'installed_applications':
    [{
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
    {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    },
        {
        'name': 'alsdfasdf',
        'version': '1',
        'installed_date': '11-11-11',
    }]
}

在这种情况下,您可以通过遍历apps,将必需的键值对添加到列表中,然后将该列表分配给installed_applications键来轻松创建值

host_dict = {}
apps = get_installed_apps(host)
host_dict['installed_applications'] = {}

#List to store list of dictionaries
li = []
#Iterate through apps
for app in apps:
    #Create a dictionary for app and append to the list
    dct = {}
    dct['name'] = app[0]
    dct['version'] = app[1]
    dct['uninstall_string'] = app[2]
    dct['install_date'] = app[3]
    dct['install_location'] = app[4]
    dct['publisher'] = app[5]
    li.append(dct)

#Assign the list
host_dict['installed_applications'] = li

或者简化代码,我们可以做到

host_dict = {}
apps = get_installed_apps(host)
host_dict['installed_applications'] = {}

#List to store list of dictionaries
li = []

#List of keys for app
app_keys = ['name', 'version', 'uninstall_string', 'install_date', 'install_location', 'publisher']

#Iterate through apps
for app in apps:
    dct = {}
    #Make the dictionary
    for idx, item in enumerate(app):
        dct[app_keys[item]] = item

    li.append(dct)

#Assign the list
host_dict['installed_applications'] = li