我想通过这个搜索" name“并返回名称,我已经有了它的代码。但是,我需要返回"1“或"4”,这样我就可以得到:
单角光源
然后,我可以将HTTP PUT与程序的其余部分一起发送到正确的设备。我找不到任何关于如何做这件事的东西,任何帮助都是非常感谢的。
names = [js[index]["name"] for index in js]
print (names)
{
"1": {
"state": {
"on": true,
"bri": 114,
"alert": "none",
"reachable": true
},
"type": "Dimmable light",
"name": "corner light",
},
"4": {
"state": {
"on": true,
"bri": 180,
"alert": "none",
"reachable": true
},
"type": "Dimmable light",
"name": "Back light",
},
"5": {
"state": {
"on": true,
"bri": 228,
"alert": "none",
"reachable": true
},
"type": "Dimmable light",
"name": "Best Bulb",
},
"7": {
"state": {
"on": false,
"bri": 254,
"alert": "none",
"reachable": false
},
"type": "Dimmable light",
"name": "our bulb",
},发布于 2017-02-02 06:14:47
只需在语句中连接索引和分隔符:
names = [index + " - " + js[index]["name"] for index in js]这给出了输出
['1 - corner light', '5 - Best Bulb', '4 - Back light', '7 - our bulb']顺便说一句,您需要将布尔值(True & False)大写以使Python满意。
https://stackoverflow.com/questions/41990632
复制相似问题