I am wanting to search through this for "name" and return the name, which I already have the code for. However, I need to return the "1" or "4" along with it so I end up with:
1 - corner light
I can then send a HTTP PUT to the correct device with the rest of my program. I can't find anything on how to go about this, any help is appreciated.
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",
},
Just concatenate the index and the separator in your statement:
names = [index + " - " + js[index]["name"] for index in js]
This gives output
['1 - corner light', '5 - Best Bulb', '4 - Back light', '7 - our bulb']
BTW, you'll need to capitalize the Boolean values (True & False) to keep Python happy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.