Python Dict-多实例或;分节;问题-以编程方式添加项目
我想用python创建一个dict,它有许多“子值”,我需要通过编程来填充这些“子值”。此dict将用于向MongoDB数据库添加文档 我最终想要使用的dict的一个想法是:Python Dict-多实例或;分节;问题-以编程方式添加项目,python,python-3.x,loops,dictionary,Python,Python 3.x,Loops,Dictionary,我想用python创建一个dict,它有许多“子值”,我需要通过编程来填充这些“子值”。此dict将用于向MongoDB数据库添加文档 我最终想要使用的dict的一个想法是: host_dict = { 'installed_applications': { 'name': 'alsdfasdf', 'version': '1', 'installed_date': '11-11-11', } {
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”(你会这么称呼它吗?)你的
host\u dict
示例不是有效的python结构,但是你可以通过将installed\u 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']
将有一个列表,其中每个值都是一个应用程序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\u dict
不是有效的字典,我将假设您试图创建一个字典,其中包含键installed\u 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',
}]
}
在这种情况下,您可以通过迭代应用程序
,将所需的键值对添加到列表中,然后将该列表分配给已安装的应用程序
键,轻松创建值
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
您对
host\u dict
的期望值不是有效的Python(请尝试!)。很好-我需要一个列表,其中每个列表都有一个dict。谢谢朋友!感谢accept@chamber,我还添加了一个短得多的代码版本!