我希望在python中创建一个具有许多“子值”(Sub)的dict,我需要通过编程来填充这些值。此dict将用于将文档添加到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”(这就是您所称的吗?)
发布于 2019-05-10 02:12:09
如果我错了,请纠正我,但是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发布于 2019-05-10 02:11:19
您的host_dict示例不是一个有效的python结构,但是您可以通过将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',
}]
}https://stackoverflow.com/questions/56069703
复制相似问题