如果值匹配,则将一个dicts数组中的特定值合并到另一个数组中

时间:2018-03-13 20:34:01

标签: python python-3.x list-comprehension dictionary-comprehension

如果单个特定值在它们之间匹配,如何将一个dicts数组中的特定值合并到另一个dicts数组中?

我有一系列代表书籍的词汇

books = [{'writer_id': '123-456-789', 'index': None, 'title': 'Yellow Snow'}, {'writer_id': '888-888-777', 'index': None, 'title': 'Python for Dummies'}, {'writer_id': '999-121-223', 'index': 'Foo', 'title': 'Something Else'}]

我有一系列代表作者的dicts

authors = [{'roles': ['author'], 'profile_picture': None, 'author_id': '123-456-789', 'name': 'Pat'}, {'roles': ['author'], 'profile_picture': None, 'author_id': '999-121-223', 'name': 'May'}]

我想从authors中取名,然后将其添加到books中的词典writer_id与作者author_id匹配。

我的最终结果理想情况下会更改dicts的书籍数组(注意第一个dict现在具有'name'的值:'Pat',第二本书有'name':'May'):

books = [{'writer_id': '123-456-789', 'index': None, 'title': 'Yellow Snow', 'name': 'Pat'}, {'writer_id': '888-888-777', 'index': None, 'title': 'Python for Dummies'}, {'writer_id': '999-121-223', 'index': 'Foo', 'title': 'Something Else', 'name': 'May'}]

我目前的解决方案是:

for book in books:
  for author in authors:
    if book['writer_id'] == author['author_id']:
      book['author_name'] = author['name']

这很有效。但是,嵌套的语句让我烦恼并且感到笨拙。我还有许多其他类似的结构,所以我最终得到一个函数,其中包含一堆类似于此的代码:

for book in books:
      for author in authors:
        if book['writer_id'] == author['author_id']:
          book['author_name'] = author['name']

books_with_foo = []
for book in books:
      for thing in things:
        if something:
          // do something


for blah in books_with_foo:
      for book_foo in otherthing:
        if blah['bar'] == stuff['baz']:
          // etc, etc.

或者,您如何将来自多个数据库表的数据聚合成一个...某些数据以字典形式返回,有些数据作为dicts数组?

1 个答案:

答案 0 :(得分:1)

熊猫几乎肯定会在这里帮助你。将您的dicts转换为DataFrames以便于操作,然后合并它们:

import pandas as pd

authors = [{'roles': ['author'], 'profile_picture': None, 'author_id': '123-456-789', 'name': 'Pat'}, {'roles': ['author'], 'profile_picture': None, 'author_id': '999-121-223', 'name': 'May'}]
books = [{'writer_id': '123-456-789', 'index': None, 'title': 'Yellow Snow'}, {'writer_id': '888-888-777', 'index': None, 'title': 'Python for Dummies'}, {'writer_id': '999-121-223', 'index': 'Foo', 'title': 'Something Else'}]

df1 = pd.DataFrame.from_dict(books)
df2 = pd.DataFrame.from_dict(authors)

df1['author_id'] = df1.writer_id
df1 = df1.set_index('author_id')
df2 = df2.set_index('author_id')

result = pd.concat([df1, df2], axis=1)

您可能会发现this page对于将DataFrames单独组合(合并,连接等)的不同方式有所帮助。