我有一个包含列表字典的函数,函数如下:
def call_openNMS_service(node):
print node
打印节点的输出是
{'ip': [u'10.21.204.174', u'10.21.205.148', u'10.21.50.153', u'10.21.50.192', u'10.21.50.198', u'10.21.51.149', u'10.21.51.158', u'10.21.51.200', u'10.21.51.252', u'10.21.52.202', u'10.21.52.53', u'10.21.54.12', u'10.21.54.149', u'10.21.55.132', u'10.21.55.176', u'10.21.55.239', u'10.21.56.169', u'10.21.56.48', u'10.21.65.106', u'10.21.65.125', u'10.21.65.34', u'10.21.67.131', u'10.21.67.179', u'10.21.67.194', u'10.21.67.230', u'10.21.67.249', u'10.21.67.45', u'10.21.67.70', u'10.21.68.127', u'10.21.68.180', u'10.21.68.73', u'10.21.69.200', u'10.21.69.38', u'10.21.70.121', u'10.21.70.56'],
'id': [u'564', u'561', u'462', u'389', u'352', u'353', u'390', u'354', u'356', u'454', u'348', u'349', u'455', u'563', u'359', u'360', u'363', u'362', u'525', u'426', u'503', u'466', u'431', u'527', u'529', u'373', u'414', u'518', u'430', u'425', u'413', u'368', u'404', u'517', u'502']}
现在我必须选择第一个IP和第一个ID,然后将其传递给URL,如下所示:>
http://localhost:8980/opennms/rest/nodes/的 564 / ipinterfaces /的 10.21.204.174 /服务
和所有其他人类似。怎么做?
答案 0 :(得分:4)
您可以{d}一起zip
dict值,然后在列表理解中使用str.format
来构建您的网址。
>>> d = {'ip': [u'10.21.204.174', u'10.21.205.148', u'10.21.50.153', u'10.21.50.192', u'10.21.50.198', u'10.21.51.149', u'10.21.51.158', u'10.21.51.200', u'10.21.51.252', u'10.21.52.202', u'10.21.52.53', u'10.21.54.12', u'10.21.54.149', u'10.21.55.132', u'10.21.55.176', u'10.21.55.239', u'10.21.56.169', u'10.21.56.48', u'10.21.65.106', u'10.21.65.125', u'10.21.65.34', u'10.21.67.131', u'10.21.67.179', u'10.21.67.194', u'10.21.67.230', u'10.21.67.249', u'10.21.67.45', u'10.21.67.70', u'10.21.68.127', u'10.21.68.180', u'10.21.68.73', u'10.21.69.200', u'10.21.69.38', u'10.21.70.121', u'10.21.70.56'],
'id': [u'564', u'561', u'462', u'389', u'352', u'353', u'390', u'354', u'356', u'454', u'348', u'349', u'455', u'563', u'359', u'360', u'363', u'362', u'525', u'426', u'503', u'466', u'431', u'527', u'529', u'373', u'414', u'518', u'430', u'425', u'413', u'368', u'404', u'517', u'502']}
>>> s = 'http://localhost:8980/opennms/rest/nodes/{}/ipinterfaces/{}/services'
>>> [s.format(i,j) for i,j in zip(d['id'], d['ip'])]
['http://localhost:8980/opennms/rest/nodes/564/ipinterfaces/10.21.204.174/services',
'http://localhost:8980/opennms/rest/nodes/561/ipinterfaces/10.21.205.148/services',
'http://localhost:8980/opennms/rest/nodes/462/ipinterfaces/10.21.50.153/services',
'http://localhost:8980/opennms/rest/nodes/389/ipinterfaces/10.21.50.192/services',
'http://localhost:8980/opennms/rest/nodes/352/ipinterfaces/10.21.50.198/services',
'http://localhost:8980/opennms/rest/nodes/353/ipinterfaces/10.21.51.149/services',
'http://localhost:8980/opennms/rest/nodes/390/ipinterfaces/10.21.51.158/services',
'http://localhost:8980/opennms/rest/nodes/354/ipinterfaces/10.21.51.200/services',
'http://localhost:8980/opennms/rest/nodes/356/ipinterfaces/10.21.51.252/services',
'http://localhost:8980/opennms/rest/nodes/454/ipinterfaces/10.21.52.202/services',
'http://localhost:8980/opennms/rest/nodes/348/ipinterfaces/10.21.52.53/services',
'http://localhost:8980/opennms/rest/nodes/349/ipinterfaces/10.21.54.12/services',
'http://localhost:8980/opennms/rest/nodes/455/ipinterfaces/10.21.54.149/services',
'http://localhost:8980/opennms/rest/nodes/563/ipinterfaces/10.21.55.132/services',
'http://localhost:8980/opennms/rest/nodes/359/ipinterfaces/10.21.55.176/services',
'http://localhost:8980/opennms/rest/nodes/360/ipinterfaces/10.21.55.239/services',
'http://localhost:8980/opennms/rest/nodes/363/ipinterfaces/10.21.56.169/services',
'http://localhost:8980/opennms/rest/nodes/362/ipinterfaces/10.21.56.48/services',
'http://localhost:8980/opennms/rest/nodes/525/ipinterfaces/10.21.65.106/services',
'http://localhost:8980/opennms/rest/nodes/426/ipinterfaces/10.21.65.125/services',
'http://localhost:8980/opennms/rest/nodes/503/ipinterfaces/10.21.65.34/services',
'http://localhost:8980/opennms/rest/nodes/466/ipinterfaces/10.21.67.131/services',
'http://localhost:8980/opennms/rest/nodes/431/ipinterfaces/10.21.67.179/services',
'http://localhost:8980/opennms/rest/nodes/527/ipinterfaces/10.21.67.194/services',
'http://localhost:8980/opennms/rest/nodes/529/ipinterfaces/10.21.67.230/services',
'http://localhost:8980/opennms/rest/nodes/373/ipinterfaces/10.21.67.249/services',
'http://localhost:8980/opennms/rest/nodes/414/ipinterfaces/10.21.67.45/services',
'http://localhost:8980/opennms/rest/nodes/518/ipinterfaces/10.21.67.70/services',
'http://localhost:8980/opennms/rest/nodes/430/ipinterfaces/10.21.68.127/services',
'http://localhost:8980/opennms/rest/nodes/425/ipinterfaces/10.21.68.180/services',
'http://localhost:8980/opennms/rest/nodes/413/ipinterfaces/10.21.68.73/services',
'http://localhost:8980/opennms/rest/nodes/368/ipinterfaces/10.21.69.200/services',
'http://localhost:8980/opennms/rest/nodes/404/ipinterfaces/10.21.69.38/services',
'http://localhost:8980/opennms/rest/nodes/517/ipinterfaces/10.21.70.121/services',
'http://localhost:8980/opennms/rest/nodes/502/ipinterfaces/10.21.70.56/services']