Python字典的索引编号每个tim都是不同的

2024-03-29 06:46:32 发布

您现在位置: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"}

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

我在检查字典里的“type”是否等于我给出的参数。你知道吗

代码:

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

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

有时“if”会通过,有时索引会更改(在重建所有内容时)并失败

如何比较type4和MyDictionary[u'components'][one of index][u'type'] 除此之外,我还需要比较键的值=value4 所以这意味着如果我们检查索引x,那么应该在那里检查key和value。你知道吗

希望不要太复杂

提前谢谢

奥哈德


Tags: idnumberdateif字典linuxtypeserial
1条回答
网友
1楼 · 发布于 2024-03-29 06:46:32

您可以通过循环组件值来进行比较,而不是给出索引号。你知道吗

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

相关问题 更多 >