简体   繁体   English

如何在列表中查找键值对的序列号

[英]How to find the sequence number of a key value pair in a list

The following code is working as expected. 以下代码按预期工作。 I know the reservation ID 50b4f837 and I could find the instance related to that reservation. 我知道预订ID 50b4f837 ,可以找到与该预订相关的实例。

>>> reservations = conn.get_all_instances()
>>> reservations
[Reservation:r-cded15ae, Reservation:r-50b4f837]

>>> instances = reservations[1].instances
>>> instances
[Instance:i-5ffecc27]

The problem is that I need to find the instance programmtically without returning all the reservations and then manually calling the specific key (1 in this case). 问题是我需要以编程方式查找实例而不返回所有保留,然后手动调用特定键(在这种情况下为1)。

I will supply the reservation ID and it should return the instance ID. 我将提供预订ID,它应该返回实例ID。

If you know the reservation ID and if all of your reservations are for a single instance, you could do this: 如果您知道预订ID,并且所有预订都针对单个实例,则可以执行以下操作:

import boto.ec2

ec2 = boto.ec2.connect_to_region('us-west-2')
instance = get_all_instances(filters={'reservation-id': 'r-50b4f837'})[0].instances[0]

Again, this would only work if your reservations always contain a single instance (ie you only call run_instances asking for a single instance. 同样,这仅在您的预订始终包含单个实例的情况下才有效(即,您仅调用run_instances要求单个实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM