[英]compare values of different keys in list of dictionaries in python
I have two lists of dictionaries as below, I want to print url based on following conditions 我有以下两个字典列表,我想根据以下条件打印网址
if remote.value of dictionaries in list 2 = name.value of dictionaries in list 1 myurl = fetch.value of dictionary in list 1 + name.value of dict in list 2 如果列表2中字典的remote.value =列表1中字典的name.value myurl =列表1中字典的fetch.value +列表2中字典的name.value
Has anyone done this before. 有人做过这个吗? with this I will get the giturl and clone the repo at revision in list 2 per dictionary 这样,我将获得giturl并在每个字典的列表2中的修订版本处克隆回购。
List of Dict - 1 词典列表-1
[
{'fetch': 'https://github.com/cbase/', 'name': 'cbase'},
{'fetch': 'https://github.com/cbasela/', 'name': 'cbaselabs'}
]
List if Dict - 2 列出是否有字典-2
[
{'remote': 'cbase', 'name': 'cbgt', 'revision': '06193c'},
{'remote': 'cbase', 'name': 'cbauth', 'revision': '1323b9'},
{'remote': 'cbasela', 'name': 'pink', 'revision': 'cfb33e'}
]
Try to use list comprehension
: 尝试使用list comprehension
:
[el1['fetch'] + el2['name'] for el1 in list1 for el2 in list2 if el1['name'] == el2['remote']]
output list of urls: 网址输出清单:
['https://github.com/cbase/cbgt', 'https://github.com/cbase/cbauth']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.