Python3如何通过唯一键连接两个dicts列表
我有两个清单: list1 = [ {'sth': 13,'important_key1': 'AA','important_key2': '3'},{'oh!': 14,'important_key1': 'FF','important_key2': '4'},{'sth_else': 'abc','important_key1': 'ZZ','important_key2': '5'}] list2 = [ {'why-not': 'tAk','important_key1': 'GG',{'hmmm': 'no','important_key2': '3'}] 我想返回一个仅包含来自list1的对象的列表,但如果同一个important_key1和important_key2在list2中的任何元素中,我想从list2中获取该元素. 所以输出应该是: [ {'hmmm': 'no','important_key2': '5'}] 通过两个或三个循环来做它并不复杂,但我想知道是否有一种简单的方法可以使用列表推导或类似的东西. 这是“正常”的方式: list1 = [ {'sth': 13,'important_key2': '4'}] list2 = [ {'hmmm': 'no',{'why-not': 'tAk','important_key2': '4'}] final_list = [] for element in list1: there_was_in_list2 = False for another_element in list2: if element['important_key1'] == another_element['important_key1'] and element['important_key2'] == another_element['important_key2']: final_list.append(another_element) there_was_in_list2 = True break if not there_was_in_list2: final_list.append(element) print(final_list) 是否有任何Pythonic方法可以做到这一点? 解决方法您可以将list2转换为由list2中的重要键的值元组索引的dict,然后使用它来确定list1中的相同键是否具有与列表推导中的list1迭代相同的值,以便时间复杂性从你的O(n * m)减少到O(n):keys = ['important_key1','important_key2'] d2 = {tuple(d[k] for k in keys): d for d in list2[::-1]} print([d2.get(tuple(d[k] for k in keys),d) for d in list1]) 这输出(带有您的样本输入): [{‘hmmm’:’不’,’important_key1’:’AA’,’important_key2’:’3′},{‘哦!’:14,’important_key1’:’FF’,’important_key2’:’4′ },{‘sth_else’:’abc’,’important_key1’:’ZZ’,’important_key2’:’5′}] 正如您在问题中所描述的那样,list1中只有{‘sth’:13,’important_key2’:’3′}将替换为{‘hmmm’:’no’,’important_key1’: ‘AA’,’important_key2’:’3′}因为只有这个dict的important_key1和important_key2都匹配list2中的dict. (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 举例详解Python中的split()函数的使用方法
- python – 将numpy.array中的每个元素与numpy.array中的每个
- Python操作串口的方法
- python – boto dynamodb2:我可以只使用范围键查询表吗?
- python – OpenCV光流断言
- python – 从py2exe’d程序连接到Oracle时出错:无法获取Or
- TypeError:’function’对象不可订阅 – Python
- 在Python Celery中,如何在连续的工作调用中持久保存对象?
- python – 是否有任何方法可以使用openpyxl获取.xlsx表中存
- 使用Python xmlrpclib与unix域套接字?
- python – TensorFlow:SKCompat折旧警告
- python – 使用scikit-learn(sklearn),如何处理线
- Django中模型Model添加JSON类型字段的方法
- python – 从defaultdict获取原始密钥集
- python – gcloud.exceptions.Forbidden:403权限
- python – 如何覆盖BaseHTTPRequestHandler log_
- python – 如果列超过特定数量的NA值,则删除该列
- python – 将临时表与SQLAlchemy一起使用
- python – Flask:后台线程看到一个非空队列为空
- python – pandas数据框 – 选择行和清除内存?