Py学习  »  Python

Python Dict-多个实例或“Sub Dict”问题-以编程方式添加项

chamber • 5 年前 • 1582 次点击  

我最终想用的一个词是:

    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”(这就是您所说的吗?)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55050
文章 [ 2 ]  |  最新文章 5 年前
gcw
Reply   •   1 楼
gcw    6 年前

你的 host_dict installed_applications 值作为列表,因此可以将项附加到此列表,每个项都是dict,如下所示:

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'] 将有一个列表,其中每个值都是 应用程序

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',
    }]
}
Devesh Kumar Singh
Reply   •   2 楼
Devesh Kumar Singh    6 年前

如果我错了就纠正我,但是 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 ,将所需的键值对添加到列表,然后将该列表分配给 钥匙

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