如果我有一个词典列表,请说:
[{'id': 1, 'name': 'paul'},
{'id': 2, 'name': 'john'}]
Run Code Online (Sandbox Code Playgroud)
我想删除id2(或名称john)的字典,这是以编程方式进行此操作的最有效方法(也就是说,我不知道列表中条目的索引,所以它可以不要弹出).
我有一封电子邮件,我正在阅读我需要修改其附件的 Python 电子邮件库。email Message 类有“attach”方法,但没有“detach”之类的东西。如何从多部分消息中删除附件?如果可能,我想在不从头开始重新创建消息的情况下执行此操作。
基本上我想:
我有一个无法转储的对象simplejson,所以我需要先从它创建一个列表.目前这就是我正在使用的:
messages = h.flash.pop_messages()
items = []
for message in messages:
item = {}
item['category'] = message.category
item['message'] = message.message
items.append(item)
Run Code Online (Sandbox Code Playgroud)
我觉得有更多的pythonic方式让我做这个,有人可以解决一些问题吗?
编辑:
根据要求,这是Message对象的类:
class Message(object):
"""A message returned by ``Flash.pop_messages()``.
Converting the message to a string returns the message text. Instances
also have the following attributes:
* ``message``: the message text.
* ``category``: the category specified when the message was created.
"""
def __init__(self, category, message):
self.category=category
self.message=message
def __str__(self):
return self.message
__unicode__ = __str__
def __html__(self): …Run Code Online (Sandbox Code Playgroud)