Python字典-索引编号每次都不同



我使用的是python 2.7\Linux

我有api结果(见下文):

{"id":"137","iv":0,
"components":[{"id":"928","type":"IDUGW","creation_date":"2016-05-04 08:10:38.0","update_date":"2016-05-04 08:10:38.0","unit_id":"137","serial_number":"00000000501C8349"},
{"id":"927","type":"IDU","creation_date":"2016-05-04 08:10:37.0","update_date":"2016-05-04 08:10:37.0","unit_id":"137","serial_number":"00000000501C8268"},
{"id":"930","type":"Battery","creation_date":"2016-05-04 08:10:40.0","update_date":"2016-05-04 08:10:40.0","unit_id":"137","serial_number":"00000000501C802A"}
,{"id":"929","type":"Panel","creation_date":"2016-05-04 08:10:39.0","update_date":"2016-05-04 08:10:39.0","unit_id":"137","serial_number":"00000000501C810B"}],
"creation_date":"2016-05-04 08:10:41.0",
"update_date":"2016-05-04 08:10:41.0",
"serial_number":"0011",
"phone_number":"972528745028",
"owner_phone":"9720545555554"}

如果我理解正确,我在字典里有字典(第二行"组件"是另一个字典,里面有4个键值)

我试着检查字典中的"type"是否等于我给出的参数。

代码:

if(MyDictionary[u'components'][0][u'type']<>lRestParams['type4']):

我有4个索引(0,1,2,3)

有时"if"通过,有时索引被更改(重建所有内容时),失败

如何将type4与MyDictionary进行比较此外,我需要在那之后比较键的值=值4所以这意味着,如果我们检查索引x,那么键和值应该在那里检查。

希望不要使复杂化

提前感谢

Ohad

您可以循环遍历组件值来进行比较,而不是给出索引号。

for keys in data['components']:
    if keys['type'] == "IDUGW":
        print "matched"

最新更新