简体   繁体   English

Python-字典的有序列表,有大小限制?

[英]Python - Ordered List of Dictionaries, With a Size Limit?

This is a requirement for a chat application project that I'm creating with Python, Flask, Socketio, and Javascript: 这是我使用Python,Flask,Socketio和Javascript创建的聊天应用程序项目的要求:

"Messages View: Once a channel is selected, the user should see any messages that have already been sent in that channel, up to a maximum of 100 messages. Your app should only store the 100 most recent messages per channel in server-side memory." “邮件视图:选择频道后,用户应该看到该频道中已发送的所有邮件,最多100条邮件。您的应用应在服务器端内存中仅将每个频道存储100条最新邮件“。

So, what I think I should be doing is creating a list of dictionaries. 因此,我认为我应该做的是创建词典列表。 Like this: 像这样:

messages = [ {"user":"Dave", "message":"Hello", "time":"12-24-2018"}, {"user":"John", "message":"Test", "time":"12-21-2018"} ]

My questions are.. 我的问题是..

  1. How do I append to/access a list of dictionaries like this? 如何附加/访问这样的词典列表?
  2. How do I limit the size of the list, and replace the oldest element with the second oldest element as I add to it? 如何限制列表的大小,并在添加到第二个最旧的元素后替换最旧的元素?
  3. Is this the best practice for storing a fixed size of data server side? 这是存储固定大小的数据服务器端的最佳实践吗?

One possible solution is to use the double-ended queue implemented in the standard library's collections package - collections.deque . 一种可能的解决方案是使用在标准库的collections package- collections.deque实现的双端队列。

Deques are similar to lists, but support efficient appending and popping from both ends, are threadsafe, and can be specified to have a maximum length. 双端队列与列表相似,但是支持两端高效的追加和弹出操作,具有线程安全性,并且可以指定为最大长度。

For example: 例如:

>>> dq = collections.deque(maxlen=5)
>>> for i, x in enumerate('abcde'):
...    dq.append({x: i})
... 
>>> dq
deque([{'a': 0}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4}], maxlen=5)

If you add an element to a deque which has maxlen set, and which is already at maximum size, an element is removed from the other end of the deque: 如果将元素添加到已设置maxlen且已达到最大大小的双端队列,则将从双端队列的另一端删除元素:

>>> dq.append({'f': 5})
>>> dq
deque([{'b': 1}, {'c': 2}, {'d': 3}, {'e': 4}, {'f': 5}], maxlen=5)

>>> dq.appendleft({'z': 25})   
>>> dq
deque([{'z': 25}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4}], maxlen=5)

It's worth noting that Python's json module doesn't handle deques, so if you want to dump the deque to json you'll need to provide a function that converts the deque to a list: 值得注意的是,Python的json模块不处理双端队列,因此,如果要将双端队列转储到json,则需要提供一个将双端队列转换为列表的函数:

>>> json.dumps(dq)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
TypeError: Object of type deque is not JSON serializable
>>> def jdq(o):
...     return list(o)
... 
>>> json.dumps(dq, default=jdq)
'[{"z": 25}, {"b": 1}, {"c": 2}, {"d": 3}, {"e": 4}]'

To recreate the deque from a json array, just pass the deserialised list to a new deque: 要从json数组重新创建双端队列,只需将反序列化的列表传递给新的双端队列:

>>> L = json.loads('[{"z": 25}, {"b": 1}, {"c": 2}, {"d": 3}, {"e": 4}]')
>>> dq = collections.deque(L, maxlen=5)
>>> dq
deque([{'z': 25}, {'b': 1}, {'c': 2}, {'d': 3}, {'e': 4}], maxlen=5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM