在for循环中是否有任何方法可以作为结果联接字典。 下面是我的示例代码:
for value in data['actions']:
if 'remoteUrls' in value:
url = value['remoteUrls']
ref = value['lastBuiltRevision']['SHA1']
new_dict['url'] = url
new_dict['ref'] = ref
print new_dict
结果:
^{pr2}$我想将结果合并到一个字典中,期望的输出如下:
{
vcs1: {
'url': [u'ssh://abc.com:29418/abc.git'],
'ref': u'194d4c418c71f77355117bd253cf2ac9849b25dd'
},
vcs2: {
'url': [u'ssh://def:29418/def.git'],
'ref': u'7a198bf01b73330c379cc54aae1631f4448a4b0b'
}
}
有没有办法达到预期的输出?任何帮助都将不胜感激。非常感谢。在
有一些简单的方法
根据你的情况
dict(dict(<key>, {'url':value['remoteUrls'], 'ref':value['lastBuiltRevision']['SHA1']} for value in data['actions'] if 'remoteUrls' in value)
这是一种方法:
或者使用注释中建议的^{} :
^{pr2}$或者使用^{} 这甚至很简单:
^{3}$相关问题 更多 >
编程相关推荐