将有条件地应用于在列表理解中迭代的数据
我有以下订单(对格式化感到抱歉,我不知道如何使其看起来可读)。我基本上有一个代表对话的对象,有时会属于属于信息,有时不属于它。
[OrderedDict([('id', 7), ('members', [OrderedDict([('id', 1), ('username', 'stefan'), ('email', '[email protected]')]), OrderedDict([('id', 10), ('username', 'Angela'), ('email', '[email protected]')])]), ('title', 'Conversation 1'), ('first_member_reveal', True), ('second_member_reveal', True), ('first_member_id', '1'), ('second_member_id', '10'), ('messages', [{'convo_id': 7, 'message': 'Conv 1 message 1', 'created_on': '03.05.2022 17:12:39', 'created_by': 1}])]), OrderedDict([('id', 23), ('members', [OrderedDict([('id', 1), ('username', 'stefan'), ('email', '[email protected]')]), OrderedDict([('id', 11), ('username', 'Sharon'), ('email', '[email protected]')])]), ('title', 'Conversation 8'), ('first_member_reveal', False), ('second_member_reveal', False), ('first_member_id', '1'), ('second_member_id', '11')])]
而且,我将以下代码应用于IT,以根据创建每个对话的最新消息的时间对字典进行排序。
sorted_data = sorted(
serialized_convos.data,
key=lambda x:
max(datetime.datetime.strptime(msg['created_on'], '%d.%m.%Y %H:%M:%S') for msg in x['messages'],
reverse=True)
该代码可以执行我想要的操作,但是如前所述,在某些情况下我要迭代的内容没有“消息”键,因此我会遇到关键错误。我尝试在列表理解中添加以下条件,但它不起作用,
max(datetime.datetime.strptime(msg['created_on'], '%d.%m.%Y %H:%M:%S') for msg in x['messages'] if 'messages'
in x.keys())
我也尝试在代码的“ for”部分且没有骰子之前添加if/else条件。
I have the following OrderedDict (I'm sorry for the formatting, I can't figure out how to get it to look readable). I basically have an object representing a conversation that sometimes has messages belonging to it and sometimes doesn't.
[OrderedDict([('id', 7), ('members', [OrderedDict([('id', 1), ('username', 'stefan'), ('email', '[email protected]')]), OrderedDict([('id', 10), ('username', 'Angela'), ('email', '[email protected]')])]), ('title', 'Conversation 1'), ('first_member_reveal', True), ('second_member_reveal', True), ('first_member_id', '1'), ('second_member_id', '10'), ('messages', [{'convo_id': 7, 'message': 'Conv 1 message 1', 'created_on': '03.05.2022 17:12:39', 'created_by': 1}])]), OrderedDict([('id', 23), ('members', [OrderedDict([('id', 1), ('username', 'stefan'), ('email', '[email protected]')]), OrderedDict([('id', 11), ('username', 'Sharon'), ('email', '[email protected]')])]), ('title', 'Conversation 8'), ('first_member_reveal', False), ('second_member_reveal', False), ('first_member_id', '1'), ('second_member_id', '11')])]
And I'm applying the following code to it in order to sort the dictionary based on the time at which the latest message belonging to each conversation was created.
sorted_data = sorted(
serialized_convos.data,
key=lambda x:
max(datetime.datetime.strptime(msg['created_on'], '%d.%m.%Y %H:%M:%S') for msg in x['messages'],
reverse=True)
The code does what I want but as mentioned, there is no 'messages' key in what I'm iterating over in some cases so I get a key error. I tried adding the following conditional in the list comprehension but it isn't working
max(datetime.datetime.strptime(msg['created_on'], '%d.%m.%Y %H:%M:%S') for msg in x['messages'] if 'messages'
in x.keys())
I also tried adding an if/else conditional before the "for" part of the code and no dice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您,您希望在最新消息在前面的同时,没有消息将对话移动到后面。
您可以检查
“消息”
键是否在字典中,如果不是,则提供一些人工旧日期。例如:打印:
If I understand you correctly, you want the conversation without messages move to the back while latest messages are in front.
You can check if
"messages"
key is in dictionary and if not, supply some artificial old date. For example:Prints: