将多个列表或项目添加到字典键

时间:2018-11-14 07:55:20

标签: python web-scraping

我不确定标题的字词,但基本上我有以下内容:

{
  "restaurantName": "Restaurant 1",
  "restaurantLink": "Link 1",
  "restaurantType": "Type 1",
  "restaurantAddress": "Address 1",
  "dateRetrieved": "14-11-2018",
  "review": {
    "reviewer": "Reviewer 1",
    "reviewDetail": "Review Detail 1"
  }
}

我正在尝试将新的review添加到评论键,因此最终输出应如下所示,具体取决于餐厅的评论数:

{
  "restaurantName": "Restaurant 1",
  "restaurantLink": "Link 1",
  "restaurantType": "Type 1",
  "restaurantAddress": "Address 1",
  "dateRetrieved": "14-11-2018",
  "review": [
    {
      "reviewer": "Reviewer 1",
      "reviewDetail": "Review Detail 1"
    },
    {
      "reviewer": "Reviewer 2",
      "reviewDetail": "Review Detail 2"
    },{
      "reviewer": "Reviewer 3",
      "reviewDetail": "Review Detail 3"
    },
    ...
    ...
  ]
}

我当前的代码是:

for review in reviews:
  HungrygowhereItem['review'] = {
      [
        'reviewer': reviewer,
        'reviewDetail': reviewDetail
      ]
  }

HungrygowhereItem是现有词典的地方

它将仅存储Reviewer 1Review Detail 1。如何将新列表添加到字典?

我发现了这个问题:Append items to dictionary,它与我的问题有关,但使用的语言不同。

3 个答案:

答案 0 :(得分:1)

首先,将键'review'的值作为列表。

>>> restaurant = {
...:  "restaurantName": "Restaurant 1",
...:  "restaurantLink": "Link 1",
...:  "restaurantType": "Type 1",
...:  "restaurantAddress": "Address 1",
...:  "dateRetrieved": "14-11-2018",
...:  "review": {
...:    "reviewer": "Reviewer 1",
...:    "reviewDetail": "Review Detail 1"
...:  }
...:}
>>> 
>>> restaurant['review'] = [restaurant['review']]
>>> restaurant['review']
>>> [{'reviewDetail': 'Review Detail 1', 'reviewer': 'Reviewer 1'}]

现在您可以追加到该列表。

>>> reviews = [{"reviewer": "Reviewer 2",
...:            "reviewDetail": "Review Detail 2"},
...:           {"reviewer": "Reviewer 3",
...:           "reviewDetail": "Review Detail 3"}]
...:           
>>> for review in reviews:
...:    restaurant['review'].append(review)
...:    
>>> restaurant['review']
>>> 
[{'reviewDetail': 'Review Detail 1', 'reviewer': 'Reviewer 1'},
 {'reviewDetail': 'Review Detail 2', 'reviewer': 'Reviewer 2'},
 {'reviewDetail': 'Review Detail 3', 'reviewer': 'Reviewer 3'}]

如果您可以在上游修复代码,也可以使restaurant成为defaultdict,而无需使用'review'键。

>>> from collections import defaultdict
>>> restaurant = {
...:  "restaurantName": "Restaurant 1",
...:  "restaurantLink": "Link 1",
...:  "restaurantType": "Type 1",
...:  "restaurantAddress": "Address 1",
...:  "dateRetrieved": "14-11-2018"
...:}
>>> 
>>> restaurant = defaultdict(list, restaurant)
>>> reviews = [{"reviewer": "Reviewer 1",
...:            "reviewDetail": "Review Detail1"},
...:           {"reviewer": "Reviewer 2",
...:            "reviewDetail": "Review Detail 2"},
...:           {"reviewer": "Reviewer 3",
...:           "reviewDetail": "Review Detail 3"}]
...:      
>>> restaurant['reviews'].extend(reviews)
>>> restaurant['reviews']
>>> 
[{'reviewDetail': 'Review Detail1', 'reviewer': 'Reviewer 1'},
 {'reviewDetail': 'Review Detail 2', 'reviewer': 'Reviewer 2'},
 {'reviewDetail': 'Review Detail 3', 'reviewer': 'Reviewer 3'}]

作为restaurant作为defaultdict的替代方案,您可以在添加任何评论之前将键'reviews'的值设置为空列表。

答案 1 :(得分:1)

您必须从略有不同的review结构开始:

restaurant = {
  ...,
  "review": []
}

请注意,review是此处的列表。

因此,向该列表添加评论只是一个基本的列表操作:

restaurant['review'].append({
    "reviewer": "Reviewer 1",
    "reviewDetail": "Review Detail 1"
})

答案 2 :(得分:1)

如果我对您的理解正确,则希望将值附加到现有字典中的数组中, 因此,例如以下字典:

_dict = {
      "restaurantName": "Restaurant 1",
      "restaurantLink": "Link 1",
      "restaurantType": "Type 1",
      "restaurantAddress": "Address 1",
      "dateRetrieved": "14-11-2018",
      "review": [
        {
          "reviewer": "Reviewer 1",
          "reviewDetail": "Review Detail 1"
        },
        {
          "reviewer": "Reviewer 2",
          "reviewDetail": "Review Detail 2"
        },{
          "reviewer": "Reviewer 3",
          "reviewDetail": "Review Detail 3"
        }
      ]
    }

将价值附加到评论中

_dict['review'].append( {
          "reviewer": "Reviewer 4",
          "reviewDetail": "Review Detail 4"
        })

在您的情况下,如果HungrygowhereItem是字典:

for review in reviews:
  if 'review' in HungrygowhereItem:
      HungrygowhereItem['review'].append({
           'reviewer': reviewer,
           'reviewDetail': reviewDetail
       })
   else:
      HungrygowhereItem['review'] = [{
           'reviewer': reviewer,
           'reviewDetail': reviewDetail
       }]