Python 如何按Boto中的CreationDate字符串对AMI进行排序?

Python 如何按Boto中的CreationDate字符串对AMI进行排序?,python,python-3.x,amazon-web-services,amazon-ec2,boto3,Python,Python 3.x,Amazon Web Services,Amazon Ec2,Boto3,使用Boto,我有: images = ec2_client.describe_images( Owners=['self'] ) 如何使用CreationDate键对这些图像进行排序 当我尝试使用: print({image['CreationDate']: image['ImageId'] in sorted(images.items(), key=lambda image: image['CreationDate'])}) 我得到类型错误:元组索引必须是整数或切片,而不是str

使用Boto,我有:

images = ec2_client.describe_images(
    Owners=['self']
)
如何使用CreationDate键对这些图像进行排序

当我尝试使用:

print({image['CreationDate']: image['ImageId'] in sorted(images.items(), key=lambda image: image['CreationDate'])})
我得到
类型错误:元组索引必须是整数或切片,而不是str

我认为这是因为CreationDate是一个字符串


可能有一些Python库需要转换?

在您的代码中,
图像
没有定义,所以它甚至不会运行。还有
images.items()
不正确。因此,脚本失败并不是因为
CreationDate
是字符串。应该是:

print({image['CreationDate']: image['ImageId'] for image in sorted(images['Images'], key=lambda image: image['CreationDate'])})
另外,
CreationDate
的格式允许将其排序为字符串。但如果您确实想将字符串解析为
datetime
,则可以执行以下操作:

from datetime import datetime
print({image['CreationDate']: image['ImageId'] for image in sorted(images['Images'], key=lambda image: datetime.strptime(image['CreationDate'], '%Y-%m-%dT%H:%M:%S.%f%z'))})


你在寻求什么样的产出?您似乎想创建一个字典,其键为创建日期,值为图像ID?或者您只是希望图像ID按日期顺序排列?