如何找到包含特定字符串和最大数字的对象?
How to find an object which contains a specific string and a number which is the highest number?
我是 Python 的初学者,我正在尝试使用 boto (CloudFormation) 列出最新创建的包含字符串的堆栈。
import boto3
client = boto3.client('cloudformation')
stacks = client.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE'
]
)
for st in stacks["StackSummaries"]:
print(st)
这是我从 运行 命令获得的输出的一部分:
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-30/4d501360-c231-11e7-8aca-50a68a0bca9a', u'StackName': 'Company-Dev-30', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 644000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Stg-22/bc2b6c10-d41a-11e7-b1ab-50d5ca789eae', u'StackName': 'Company-Stg-22', u'CreationTime': datetime.datetime(2017, 11, 28, 9, 1, 34, 985000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-64/0e85cc00-9602-11e7-8c08-50d5ca0184d2', u'StackName': 'Company-Prod-US-API-64', u'CreationTime': datetime.datetime(2017, 9, 10, 8, 28, 43, 598000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-63/1ba257c0-9600-11e7-821f-503f20f2ad4a', u'StackName': 'Company-Prod-US-API-63', u'CreationTime': datetime.datetime(2017, 9, 10, 8, 14, 46, 602000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-61/9252a9d0-2ace-11e7-8eea-503ac9ec2461', u'StackName': 'Company-Prod-US-API-61', u'CreationTime': datetime.datetime(2017, 4, 26, 22, 20, 36, 473000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-60/1ba83440-2acd-11e7-a0c3-503a90a9c435', u'StackName': 'Company-Prod-US-API-60', u'CreationTime': datetime.datetime(2017, 4, 26, 22, 10, 7, 890000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
似乎输出已经按照最新的对象排在第一位。
我想解析包含单词 "Dev" 的堆栈的名称,并且在相同环境 (Dev) 的其余堆栈中数字最高,在本例中为 "Company-Dev-31".
如何实现?
您可以使用 in
测试是否存在某些字符,如下所示:
import boto3
client = boto3.client('cloudformation')
stacks = client.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE'
]
)
for st in stacks["StackSummaries"]:
if 'dev' in st['StackName'].lower():
print(st)
这会给你:
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-30/4d501360-c231-11e7-8aca-50a68a0bca9a', u'StackName': 'Company-Dev-30', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 644000), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
首先转换为小写具有使测试用例不敏感的效果,因此 Dev
或 dev
将匹配。
板材,
首先用名称中包含 Dev 的那些堆栈创建一个子列表,然后使用 Dev 编号作为关键字对结果列表进行排序。
# make a list of stacks with Dev in stack ID
sublist = [ s for s in stacks if 'Dev' in s['StackId'] ]
# Extract a number from stack ID
REGEX = re.compile("Dev-([0-9]+)")
def extractNum(stack):
match = REGEX.search(stack['StackId'])
if match:
return int(match.group(1))
else:
return None
# Sort the stack list in descending order
sublist.sort(key=extractNum, reverse=True)
print(sublist[0])
如果您愿意,可以使用内置函数和 lambda 将其压缩为 'one liner'。
REGEX = re.compile("Dev-([0-9]+)")
sublist = sorted(filter(lambda s: 'Dev' in s['StackId'],
stacks),
key = lambda s: int(REGEX.search(s['StackId']).group(1)),
reverse = True
);
filter
生成一个迭代器,它只包含 stacks
中满足条件
的那些元素
sorted
使用生成排序值的指定函数生成排序列表。
要添加到 Martin 的答案中,您可以从包含 'Dev' 的每个条目中获取数字,然后使用列表理解来检索具有最高数字的条目:
numlist = []
for item in st:
if 'Dev' in item['StackName']:
num = int(item['StackName'].split('Dev-')[1])
numlist.append(num)
result = [i for i in st if ('Dev-%s' % max(numlist)) in i['StackName']]
print(result)
给予:
[{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}]
我是 Python 的初学者,我正在尝试使用 boto (CloudFormation) 列出最新创建的包含字符串的堆栈。
import boto3
client = boto3.client('cloudformation')
stacks = client.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE'
]
)
for st in stacks["StackSummaries"]:
print(st)
这是我从 运行 命令获得的输出的一部分:
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-30/4d501360-c231-11e7-8aca-50a68a0bca9a', u'StackName': 'Company-Dev-30', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 644000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Stg-22/bc2b6c10-d41a-11e7-b1ab-50d5ca789eae', u'StackName': 'Company-Stg-22', u'CreationTime': datetime.datetime(2017, 11, 28, 9, 1, 34, 985000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-64/0e85cc00-9602-11e7-8c08-50d5ca0184d2', u'StackName': 'Company-Prod-US-API-64', u'CreationTime': datetime.datetime(2017, 9, 10, 8, 28, 43, 598000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-63/1ba257c0-9600-11e7-821f-503f20f2ad4a', u'StackName': 'Company-Prod-US-API-63', u'CreationTime': datetime.datetime(2017, 9, 10, 8, 14, 46, 602000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-61/9252a9d0-2ace-11e7-8eea-503ac9ec2461', u'StackName': 'Company-Prod-US-API-61', u'CreationTime': datetime.datetime(2017, 4, 26, 22, 20, 36, 473000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Prod-US-API-60/1ba83440-2acd-11e7-a0c3-503a90a9c435', u'StackName': 'Company-Prod-US-API-60', u'CreationTime': datetime.datetime(2017, 4, 26, 22, 10, 7, 890000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines'}
似乎输出已经按照最新的对象排在第一位。
我想解析包含单词 "Dev" 的堆栈的名称,并且在相同环境 (Dev) 的其余堆栈中数字最高,在本例中为 "Company-Dev-31".
如何实现?
您可以使用 in
测试是否存在某些字符,如下所示:
import boto3
client = boto3.client('cloudformation')
stacks = client.list_stacks(
StackStatusFilter=[
'CREATE_COMPLETE'
]
)
for st in stacks["StackSummaries"]:
if 'dev' in st['StackName'].lower():
print(st)
这会给你:
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-30/4d501360-c231-11e7-8aca-50a68a0bca9a', u'StackName': 'Company-Dev-30', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 644000), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}
首先转换为小写具有使测试用例不敏感的效果,因此 Dev
或 dev
将匹配。
板材,
首先用名称中包含 Dev 的那些堆栈创建一个子列表,然后使用 Dev 编号作为关键字对结果列表进行排序。
# make a list of stacks with Dev in stack ID
sublist = [ s for s in stacks if 'Dev' in s['StackId'] ]
# Extract a number from stack ID
REGEX = re.compile("Dev-([0-9]+)")
def extractNum(stack):
match = REGEX.search(stack['StackId'])
if match:
return int(match.group(1))
else:
return None
# Sort the stack list in descending order
sublist.sort(key=extractNum, reverse=True)
print(sublist[0])
如果您愿意,可以使用内置函数和 lambda 将其压缩为 'one liner'。
REGEX = re.compile("Dev-([0-9]+)")
sublist = sorted(filter(lambda s: 'Dev' in s['StackId'],
stacks),
key = lambda s: int(REGEX.search(s['StackId']).group(1)),
reverse = True
);
filter
生成一个迭代器,它只包含 stacks
中满足条件
sorted
使用生成排序值的指定函数生成排序列表。
要添加到 Martin 的答案中,您可以从包含 'Dev' 的每个条目中获取数字,然后使用列表理解来检索具有最高数字的条目:
numlist = []
for item in st:
if 'Dev' in item['StackName']:
num = int(item['StackName'].split('Dev-')[1])
numlist.append(num)
result = [i for i in st if ('Dev-%s' % max(numlist)) in i['StackName']]
print(result)
给予:
[{u'StackId': 'arn:aws:cloudformation:us-west-2:AWSACCOUNTID:stack/Company-Dev-31/4d501360-d521-11e7-9aff-50a68a0bca9a', u'StackName': 'Company-Dev-31', u'CreationTime': datetime.datetime(2017, 11, 29, 16, 21, 6, 636000, tzinfo=tzutc()), u'StackStatus': 'CREATE_COMPLETE', u'TemplateDescription': 'Company - Formation of Server Machines @ Oregon (us-west-2)'}]