簡體   English   中英

Python目錄-多個實例或“子目錄”問題-以編程方式添加項目

[英]Python Dict - Multiple Instances or “Sub-Dict” Question - Adding items programmatically

我想在python中創建一個字典,該字典具有許多需要以編程方式填充的“子值”。 該字典將用於將文檔添加到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]

問題在於它不會追加應用程序的每個實例,而只是繼續覆蓋一個“ sub-dict”(就是您所稱的那個?)。

您的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',
    }]
}

糾正我,如果我錯了,但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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM