用Python方式比较两个字典列表中的值 [英] Pythonic Way to Compare Values in Two Lists of Dictionaries

查看:762
本文介绍了用Python方式比较两个字典列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,并且仍在尝试使用Python时摆脱C ++编码技术,因此,如果这是一个琐碎的问题,请原谅我.我似乎找不到最Python化的方法.

I'm new to Python and am still trying to tear myself away from C++ coding techniques while in Python, so please forgive me if this is a trivial question. I can't seem to find the most Pythonic way of doing this.

我有两个字典.两个列表中的单个字典可能包含嵌套的字典. (如果您好奇的话,实际上是一些Yelp数据.)字典的第一个列表包含这样的条目:

I have two lists of dicts. The individual dicts in both lists may contain nested dicts. (It's actually some Yelp data, if you're curious.) The first list of dicts contains entries like this:

{business_id': 'JwUE5GmEO-sH1FuwJgKBlQ',
 'categories': ['Restaurants'],
 'type': 'business'
 ...}

第二个字典列表包含这样的条目:

The second list of dicts contains entries like this:

{'business_id': 'vcNAWiLM4dR7D2nwwJ7nCA',
 'date': '2010-03-22',
 'review_id': 'RF6UnRTtG7tWMcrO2GEoAg',
 'stars': 2,
 'text': "This is a basic review",
 ...}

我想做的是提取第二个列表中与第一个列表中的特定类别匹配的所有条目.例如,如果我对餐厅感兴趣,我只希望第二个列表中的整个内容,其中business_id与第一个列表中的business_id匹配,并且单词Restaurants出现在categories的值列表中

What I would like to do is extract all the entries in the second list that match specific categories in the first list. For example, if I'm interested in restaurants, I only want the entires in the second list where the business_id matches the business_id in the first list and the word Restaurants appears in the list of values for categories.

如果我将这两个列表作为SQL中的表,我将对business_id属性进行联接,然后只需一个简单的过滤器即可获取所需的行(其中Restaurants IN categories或类似的内容).

If I had these two lists as tables in SQL, I'd do a join on the business_id attribute then just a simple filter to get the rows I want (where Restaurants IN categories, or something similar).

这两个列表非常大,因此我同时遇到了效率和内存空间问题.在我将所有这些推入SQL数据库之前,有人可以给我一些指针吗?我已经把熊猫弄得一团糟,所以我对此的经验有限.我在合并过程中遇到了麻烦.

These two lists are extremely large, so I'm running into both efficiency and memory space issues. Before I go and shove all of this into a SQL database, can anyone give me some pointers? I've messed around with Pandas some, so I do have some limited experience with that. I was having trouble with the merge process.

推荐答案

假设您的列表名为l1l2:

[each for each in l1]

l1中所有具有Restaurant类别的元素:

All elements from l1 with the Restaurant category:

[each for each in l1
      if 'Restaurants' in each['categories']]

l2中的所有元素与idRestaurant类别中的l1元素匹配:

All elements from l2 matching id with elements from l1 with the Restaurant category:

[x for each in l1 for x in l2 
   if 'Restaurants' in each['categories']
   and x['business_id'] == each['business_id'] ]

这篇关于用Python方式比较两个字典列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆