正在搜索文件并返回2个字符串

2024-10-03 23:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过这个搜索“name”并返回名称,我已经有了它的代码。但是,我需要同时返回“1”或“4”,以便最终得到:

1-角灯

然后,我可以将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",
},

Tags: namenonefalsetrueindexnamesontype
1条回答
网友
1楼 · 发布于 2024-10-03 23:20:38

只需在语句中连接索引和分隔符:

names = [index + " - " + js[index]["name"] for index in js]

这就产生了输出

['1 - corner light', '5 - Best Bulb', '4 - Back light', '7 - our bulb']

顺便说一句,您需要将布尔值大写(True&;错误)以保持Python的快乐

相关问题 更多 >