简体   繁体   中英

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.

>>> 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).

I will supply the reservation ID and it should return the instance ID.

If you know the reservation ID and if all of your reservations are for a single instance, you could do this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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