小编Mar*_*ere的帖子

在 python 中迭代字典值的最快方法是什么?

所以,我想知道迭代字典的值并将其值与另一个变量进行比较的最快方法是什么。我有一个非常简单的特定字典结构:

"data": [
    {
     "id": "xxxxxxxxxxxxx"
    },
    {
     "id": "xxxxxxxxxxxxx"
    },
    {
     "id": "xxxxxxxxxxxxx"
    },
    {
     "id": "xxxxxxxxxxxxx"
    },
    {
     "id": "xxxxxxxxxxxxx"
    },
    {
     "id": "xxxxxxxxxxxxx"
    }
]
Run Code Online (Sandbox Code Playgroud)

我已经循环遍历some_dict['data'],然后['id']使用以下代码比较它的值:

for item in some_dict:
    if item['id'] == some_value:
        #do stuff
Run Code Online (Sandbox Code Playgroud)

但是对于大尺寸的字典,需要花费很多时间,所以我很好奇其他方法来完成我想要的事情。我听说 set 适合大型列表迭代,但是有没有办法从我的 dict 结构中使用它?

json dictionary loops python-3.x

2
推荐指数
1
解决办法
7341
查看次数

标签 统计

dictionary ×1

json ×1

loops ×1

python-3.x ×1