num = '2'
data = [{'createdOn': 1390832126, 'number': '1', 'parents': ['06d23ac46b38fae3c2ad3e83d1ab4b3ff4ce83ec'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/1', 'revision': '42a52d7d4d8695c7c10ef9d83564271a5e7306a4'}, {'createdOn': 1390888571, 'number': '2', 'parents': ['06d23ac46b38fae3c2ad3e83d1ab4b3ff4ce83ec'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/2', 'revision': '6f2748aecda883a8d7530cd88b5d7bd9b33e82cd'}, {'createdOn': 1391668191, 'number': '3', 'parents': ['dacd3763724d7013abb575ef83b257376e5f3436'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/3', 'revision': '92ea14c014e02790fba349757b94b0a7403f745e'}, {'createdOn': 1392536602, 'number': '4', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/4', 'revision': '3eefc508693c54ff0e9a84424f0056ad46f89fce'}, {'createdOn': 1392639339, 'number': '5', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/5', 'revision': '833084a2f839db8a2df3a12b5581416fffabe5bc'}, {'createdOn': 1392699314, 'number': '6', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/6', 'revision': '8fa212ae0800a6e122db930a4b4f0563571e552f'}, {'createdOn': 1392714638, 'number': '7', 'parents': ['a879167cd64d1ade907a5a3eb5a64214c898ffb6'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/7', 'revision': 'c26ae0319a46b8b9c113c168ce81ff31ec548d26'}, {'createdOn': 1392786006, 'number': '8', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/8', 'revision': '4abf76d78c6a777f468b0a05a0241f9bd77b255a'}, {'createdOn': 1392786697, 'number': '9', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/9', 'revision': '6d45c22247248318e73bbfe859a01daf0268bde9'}, {'createdOn': 1392786957, 'number': '10', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/10', 'revision': '9ea6792fce255c5c713d403c06ef628d0ef1291d'}, {'createdOn': 1392787099, 'number': '11', 'parents': ['7bd0b871b375de14a2fa2a999bbab2c9e5224a9d'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/11', 'revision': '57fb7ea79496e3403ecd82c2e7d8e38ca8afc6af'}, {'createdOn': 1393906615, 'number': '12', 'parents': ['9e0c468566a8fbf4cebf658f1ebff59d1cdcda22'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/12', 'revision': '8401805fa8cfe15d5ec9d3129e25897dfa3d3935'}, {'createdOn': 1393911395, 'number': '13', 'parents': ['9e0c468566a8fbf4cebf658f1ebff59d1cdcda22'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/13', 'revision': '26755f93581e660c217523096e958d81c9c8d12e'}, {'createdOn': 1393911499, 'number': '14', 'parents': ['9e0c468566a8fbf4cebf658f1ebff59d1cdcda22'], 'uploader': {'username': 'gasati', 'name': 'Gaurav Asati', 'email': 'gasati@codeaurora.org'}, 'ref': 'refs/changes/34/603234/14', 'revision': '9ab6a2f41bef5848906a5ce597322cc1c88457ea'}]
我希望匹配' num'变量为'数字'数据中的价值'然后我想得到' ref'对于相应的数字,例如此处的值为' num'变量是' 2'所以,我需要匹配数字:' 2'并获得相应的参考
在这种情况下,输出应如下
OUTPUT:-refs/changes/34/603234/2
答案 0 :(得分:3)
您可以使用列表理解:
>>> num = '2'
>>> [d['ref'] for d in data if d['number'] == num]
['refs/changes/34/603234/2']
或与上面相同的for循环:
refs = []
for d in data:
if d['number'] == num:
refs.append(d['ref'])
如果数据只能包含1个参考,或者您只需要1:
for d in data:
if d['number'] == num:
ref = d['ref']
break
print ref
答案 1 :(得分:1)
[d['ref'] for d in data if d['number'] == num][0]