从字典列表中找到最早的创建日期和键ID,并以字典形式返回结果。

huangapple go评论72阅读模式
英文:

find oldest createdate and keyid from list of dicts and return result in a dict

问题

我从系统中得到以下响应:

{'KeyMetadata':
[
    {
        'UserName': 'thisusernameA',
        'KeyId': 'ABCDE12345',
        'CreateDate': datetime.datetime(2022,11, 30, 13, 7, 43, tzinfo=tzutc())
    },
    {
        'UserName': 'thisusernameA',
        'KeyId': 'CDEFG678911',
        'CreateDate': datetime.datetime(2022,12, 9, 14, 50, 36, tzinfo=tzutc())
    }
]
}

所以用户有两个不同的密钥,创建日期不同。
我需要从最旧的密钥中隔离出KeyId和CreateDate,并返回以下格式的字典:

{'KeyId': 'ABCDE12345', 'CreateDate': datetime.datetime(2022,11, 30, 13, 7, 43, tzinfo=tzutc())}

我似乎已经找到了至少获取最旧日期的方法:

mylist = my_result["KeyMetadata"]
seq = [x['CreateDate'] for x in mylist]
my_oldest = min(seq)

但接下来我该怎么做?如何获取相关的KeyId并构建字典响应?

英文:

I get below response from a system:

{'KeyMetadata':
[
	{
		'UserName': 'thisusernameA',
		'KeyId': 'ABCDE12345',
		'CreateDate': datetime.datetime(2022,11, 30, 13, 7, 43, tzinfo=tzutc())
	},
	{
		'UserName': 'thisusernameA',
		'KeyId': 'CDEFG678911',
		'CreateDate': datetime.datetime(2022,12, 9, 14, 50, 36, tzinfo=tzutc())
	}
]
}

so the user has two different keys, with a different createdate.
I have to isolate the KeyId and CreateDate from the oldest key, and return the result in a dict like this:

{'KeyId': 'ABCDE12345', 'CreateDate': datetime.datetime(2022,11, 30, 13, 7, 43, tzinfo=tzutc())}

I seems that I have figured out a way to at least get the oldest date:

mylist=my_result["KeyMetadata"]
seq = [x['CreateDate'] for x in mylist]
my_oldest=min(seq)

But what do I do next? how do I get the relating KeyId and construct a dict response?

答案1

得分: 2

不要分离数据,使用自定义键min来使用日期

data = {'KeyMetadata': [
    {'UserName': 'thisusernameA', 'KeyId': 'ABCDE12345',
     'CreateDate': datetime.datetime(2022, 11, 30, 13, 7, 43, tzinfo=tzutc())},
    {'UserName': 'thisusernameA', 'KeyId': 'CDEFG678911',
     'CreateDate': datetime.datetime(2022, 12, 9, 14, 50, 36, tzinfo=tzutc())}
]}

min_item = min(data['KeyMetadata'], key=lambda item: item['CreateDate'])
print(min_item)
# {'UserName': 'thisusernameA', 'KeyId': 'ABCDE12345', 'CreateDate': datetime.datetime(2022, 11, 30, 13, 7, 43, tzinfo=tzutc())}
英文:

Don't separate the data, use min with a custom key to use the date

data = {'KeyMetadata': [
    {'UserName': 'thisusernameA', 'KeyId': 'ABCDE12345',
     'CreateDate': datetime.datetime(2022, 11, 30, 13, 7, 43, tzinfo=tzutc())},
    {'UserName': 'thisusernameA', 'KeyId': 'CDEFG678911',
     'CreateDate': datetime.datetime(2022, 12, 9, 14, 50, 36, tzinfo=tzutc())}
]}

min_item = min(data['KeyMetadata'], key=lambda item: item['CreateDate'])
print(min_item)
# {'UserName': 'thisusernameA', 'KeyId': 'ABCDE12345', 'CreateDate': datetime.datetime(2022, 11, 30, 13, 7, 43, tzinfo=tzutc())}

huangapple
  • 本文由 发表于 2023年1月8日 21:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定