如何从di中提取和组合值

2024-04-26 20:46:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个保存列表的函数,函数如下所示:

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/i接口/10.21.204.174/services

其他人也一样。怎么做


Tags: 函数gtipidnodeurl列表节点
1条回答
网友
1楼 · 发布于 2024-04-26 20:46:55

您可以zip将dict值放在一起,然后在列表理解中使用str.format来构建您的url

>>> 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']

相关问题 更多 >