【发布时间】:2018-01-25 03:24:27
【问题描述】:
所以,我想知道迭代字典值并将其值与另一个变量进行比较的最快方法是什么。我有一个非常简单的特定字典结构:
"data": [
{
"id": "xxxxxxxxxxxxx"
},
{
"id": "xxxxxxxxxxxxx"
},
{
"id": "xxxxxxxxxxxxx"
},
{
"id": "xxxxxxxxxxxxx"
},
{
"id": "xxxxxxxxxxxxx"
},
{
"id": "xxxxxxxxxxxxx"
}
]
我已经遍历some_dict['data'],然后使用此代码比较它的['id'] 值:
for item in some_dict:
if item['id'] == some_value:
#do stuff
但是对于一个大尺寸的字典,这需要很多时间,所以我很好奇其他方法来做我想做的事。我听说 set 对大型列表迭代很有用,但无论如何可以从我的 dict 结构中使用它?
【问题讨论】:
标签: json python-3.x loops dictionary