簡體   English   中英

在 Python 中創建字典列表

[英]Create a list of dictionaries in Python

我正在嘗試創建一個字典列表,但無法將我的字典推送到列表中。 我犯了什么錯誤。

數據 (mongo_data) 的外觀:

{
 'url': 'https://goodreads.com/',
 'variables': [{'key': 'Harry Potter', 'value': '10.0'},
               {'key': 'Discovery of Witches', 'value': '8.5'},],
 'vendor': 'Fantasy' 
 }

 {
 'url': 'https://goodreads.com/',
 'variables': [{'key': 'Hunger Games', 'value': '10.0'},
               {'key': 'Maze Runner', 'value': '5.5'},],
 'vendor': 'Dystopia' 
 }

 {
 'url': 'https://kindle.com/',
 'variables': [{'key': 'Twilight', 'value': '5.9'},
               {'key': 'Lord of the Rings', 'value': '9.0'},],
 'vendor': 'Fantasy' 
 }

 {
 'url': 'https://kindle.com/',
 'variables': [{'key': 'The Handmaids Tale', 'value': '10.0'},
               {'key': 'Divergent', 'value': '9.0'},],
 'vendor': 'Fantasy' 
 }

我從 MongoDB 獲得的數據:

for item in mongo_data:
    url = item['url']
    genre = item['genre']
    books = item['books']

我的代碼:

url_array = []
url_array.append(url)
unique_urls = set(url_array)
searches = []
main_dict = {}
searches.append(main_dict)
results = []

for url in list(unique_urls):
    book_vals = {}
    main_dict['url'] = url
    main_dict['results'] = [book_vals]
    results.append(book_vals)
    book_vals['genre'] = genre
    book_vals['data'] = books

我的結果:

    {
    "searches": [
        {
            "url": "http://goodreads.com",
            "results": [
                {
                    "genre": "Fantasy",
                    "data": [
                        {
                            "name": "Harry Potter",
                            "value": "10.0"
                        },
                        {
                            "name": "Discovery of Witches",
                            "value": "8.5"
                        },
                    ]
                }
            ]
        },
        {
            "url": "http://goodreads.com",
            "results": [
                {
                    "genre": "Dystopia",
                    "data": [
                        {
                            "name": "Hunger Games",
                            "value": "10.0"
                        },
                        {
                            "name": "Maze Runner",
                            "value": "5.5"
                        }
                    ]
                }
            ]
        }, 
        {
            "url": "http://kindle.com",
            "results": [
                {
                    "genre": "Fantasy",
                    "data": [
                        {
                            "name": "Twilight",
                            "value": "5.9"
                        },
                        {
                            "name": "Lord of the Rings",
                            "value": "9.0"
                        },
                    ]
                }
            ]
        },
        {
            "url": "http://kindle.com",
            "results": [
                {
                    "genre": "Dystopia",
                    "data": [
                        {
                            "name": "The Handmaids Tale",
                            "value": "10.0"
                        },
                        {
                            "name": "Divergent",
                            "value": "9.0"
                        }
                    ]
                }
            ]
        }
    ]
}

一切都被添加到搜索數組中。

但是我需要首先將它們按main_dict 中url分組,然后再按流派分組的結果

預期成績:

{
    'searches': [
        { 
            'url': 'http://goodreads.com',
            'results': [
               {
                    'genre': 'Fantasy',
                    'data': [
                        {
                            'key': 'Harry Potter',
                            'value': '10.0'
                        }, {
                            'key': 'Discovery of Witches',
                            'value': '8.5'
                        }
                    ]
                }, {
                    'genre': 'Dystopia',
                    'data': [{
                            'key': 'Hunger Games',
                            'value': '10.0'
                        }, {
                            'key': 'Maze Runner',
                            'value': '5.5'
                        }
                    ]
                }
            ] 
        } ,
        { 
            'url': 'http://kindle.com',
            'results': [
               {
                    'genre': 'Fantasy',
                    'data': [
                        {
                            'key': 'Twilight',
                            'value': '5.9'
                        }, {
                            'key': 'Lord of the Rings',
                            'value': '9.0'
                        }
                    ]
                }, {
                    'genre': 'Dystopia',
                    'data': [{
                            'key': 'The Handmaids Tale',
                            'value': '10.0'
                        }, {
                            'key': 'Divergent',
                            'value': '9.0'
                        }
                    ]
                }
            ] 
        } 
    ]
}

對於任何數據結構問題,我們深表歉意。

請嘗試以下操作。 關鍵是使用groupby將具有相同 URL 的項目組合在一起。

mongo_data = [{
 'url': 'https://goodreads.com/',
 'variables': [{'key': 'Harry Potter', 'value': '10.0'},
               {'key': 'Discovery of Witches', 'value': '8.5'},],
 'vendor': 'Fantasy' 
 },{
 'url': 'https://goodreads.com/',
 'variables': [{'key': 'Hunger Games', 'value': '10.0'},
               {'key': 'Maze Runner', 'value': '5.5'},],
 'vendor': 'Dystopia' 
 },{
 'url': 'https://kindle.com/',
 'variables': [{'key': 'Twilight', 'value': '5.9'},
               {'key': 'Lord of the Rings', 'value': '9.0'},],
 'vendor': 'Fantasy' 
 },{
 'url': 'https://kindle.com/',
 'variables': [{'key': 'The Handmaids Tale', 'value': '10.0'},
               {'key': 'Divergent', 'value': '9.0'},],
 'vendor': 'Fantasy' 
 }]

from itertools import groupby, chain
import json

searches = []
for key, group in groupby(mongo_data, key=lambda chunk: chunk['url']):
    search = {}
    search["url"] = key
    search["results"] = [{"genre": result["vendor"], "data": result["variables"]} for result in group]
    searches.append(search)

print(json.dumps(searches))

輸出

[
  {
    "url": "https://goodreads.com/",
    "results": [
      {
        "genre": "Fantasy",
        "data": [
          {
            "key": "Harry Potter",
            "value": "10.0"
          },
          {
            "key": "Discovery of Witches",
            "value": "8.5"
          }
        ]
      },
      {
        "genre": "Dystopia",
        "data": [
          {
            "key": "Hunger Games",
            "value": "10.0"
          },
          {
            "key": "Maze Runner",
            "value": "5.5"
          }
        ]
      }
    ]
  },
  {
    "url": "https://kindle.com/",
    "results": [
      {
        "genre": "Fantasy",
        "data": [
          {
            "key": "Twilight",
            "value": "5.9"
          },
          {
            "key": "Lord of the Rings",
            "value": "9.0"
          }
        ]
      },
      {
        "genre": "Fantasy",
        "data": [
          {
            "key": "The Handmaids Tale",
            "value": "10.0"
          },
          {
            "key": "Divergent",
            "value": "9.0"
          }
        ]
      }
    ]
  }
]

所以,如果這是你的代碼,它沒有多大意義。 (我假設由於某種原因你沒有分享你的實際代碼?)

url_array = []
url_array.append(url)
# so- your url_array only has one url?
unique_urls = set(url_array)
searches = []
main_dict = {}
searches.append(main_dict)
# searches will only contain one dict?
results = []

for url in list(unique_urls):
    book_vals = {}
    main_dict['url'] = url
    # as written, you would be over-writing the values in 'main_dict' every time
    main_dict['results'] = [book_vals]
    results.append(book_vals)
    book_vals['genre'] = genre
    book_vals['data'] = books

相反,讓我談談關於這個問題的一些更一般的事情。 你說

But I need them to be grouped by first the url in the main_dict and then again the results to be grouped by genre

如果我們想獲取您的搜索結果並將它們分組兩次,我會這樣做。

class SearchResult:
    url: str
    title: str
    genre: str

result_factory = lambda: {data: []}
search_factory = lambda: {results: default_dict(result_factory)}
searches = default_dict(search_factory)

for search in search_data:
    searches[search.url][search.genre].append(search.title)

基本思想是,在內容進行分組時,您可以使用字典。 所以要按 url 對搜索進行分組,你有一個 url 字典到結果集合。 因為你想要它嵌套,所以有一個 url 字典到一個流派字典到一個標題列表。

默認的 dict 內容只是語法糖,用於快速啟動每個記錄,而不是檢查它是否存在並在必要時添加空對象。

暫無
暫無

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

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