【发布时间】:2019-09-27 21:31:33
【问题描述】:
我想在 python 中创建一个字典,它有许多我需要以编程方式填充的“子值”。此 dict 将用于将文档添加到 MongoDB 数据库。
我最终想要使用的 dict 的一个想法是:
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]
问题是它没有附加应用程序的每个实例,它只是覆盖了一个“子字典”(这就是你所说的吗?)
【问题讨论】:
-
您对
host_dict的期望值不是有效的 Python(试试吧!)。
标签: python python-3.x loops dictionary