python - 如何安全地计算 python 中的字典键

标签 python django

<分区>

我正在编写一个 Django 应用程序,我将从用户那里获得一个大小可变的字典。我想限制字典的大小,即它可以容纳多少 (key, value) 对。我希望它不超过 200。我怀疑如果这样做:

if len(user_dict)>200:
    raise ValidationError("dict has too many (key, value) pairs")

python 必须对整个字典进行计数。如果 dict 很大,因为恶意用户,这将消耗不必要的处理能力。或者 dict 是否跟踪它拥有多少个对象,这意味着 len(user_dict) 是一个简单的查找操作?解决此问题的最佳方法是什么?

我在想:

i=0
for key in user_dict.keys():
    i += 1
    if i>200:
        raise ValidationError("dict has too many (key, value) pairs")

最佳答案

Or does the dict keep track of how many objects it holds, meaning len(user_dict) is a simple lookup operation?

字典 - 给定像 CPython 这样的严肃的解释器实现 - 实际上会跟踪存储在字典中的键值对的数量。所以如果user_dict确实是一个字典,那么len(user_dict)O(1)中工作并且非常快速地。它在恒定时间内工作的事实也意味着无论我们计算具有 100k 项的 dict 对象的 len(..) 没有(理论上的)区别,或者完全没有。

不需要迭代来计算对象的数量。例如 CPython source code for the dict class has :

static Py_ssize_t
dict_length(PyDictObject *mp)
{
    return mp->ma_used;
}

因此它返回字典对象的 ma_used 字段(因此这是一个包含字典中项目数的字段)。

this file 中也对此进行了描述:

Dictionaries: dict and defaultdict
                               Complexity
Operation     | Example      | Class         | Notes
--------------+--------------+---------------+-------------------------------
Index         | d[k]         | O(1)      |
Store         | d[k] = v     | O(1)      |
Length        | len(d)       | O(1)      |
Delete        | del d[k]     | O(1)      |
get/setdefault| d.method     | O(1)      |
Pop           | d.pop(k)     | O(1)      |
Pop item      | d.popitem()  | O(1)      |
Clear         | d.clear()    | O(1)      | similar to s = {} or = dict()
View          | d.keys()     | O(1)      | same for d.values()

Construction  | dict(...)    | O(len(...))   | depends # (key,value) 2-tuples

Iteration     | for k in d:  | O(N)          | all forms: keys, values, items

关于python - 如何安全地计算 python 中的字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096415/

相关文章:

django - 在序列化程序 Django 中使用分页

python - 如何限制 Django raw_id_field 的 ForeignKey 选择的选择

python - Pandas:astype错误字符串到 float (无法将字符串转换为 float : '7,50')

python - setup.py 检查是否存在非python库依赖

python - FTP 下载,带有显示当前下载状态的文本标签

Python:如何查找数组中特定元素的索引?

python - pip 安装django-allauth UnicodeDecodeError

html - 使用 Django 表单时从单选按钮中删除元素符号

python - redis celeryd 和 apache

python - 将键复制到新字典 (Python)