[英]How to get values from dictionary in python?
我想为type
为String
entity_id with attribute
添加entity_id with attribute
值。 下面是代码的详细信息:
entities = [{'id': 'Room1',
'temperature': {'value': '10', 'type': 'String'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346348'},
{'id': 'Room2',
'temperature': {'value': '10', 'type': 'Number'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346664'},
{'id': 'Room3',
'temperature': {'value': '10', 'type': 'Number'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Array',
'time_index': '2020-08-24T06:23:37.346664'}]
attr = ['temperature','pressure']
self.logger.warning(msg.format( attr, entity_id)
我如何添加id
值,其中类型为String
并在上面的代码中带有warning msg.format
.. 新来的 python 所以任何帮助都会很棒。谢谢
要获取字典键的值,请执行以下操作。
dict = {'a':1, 'b':2}
print(dict['a'])
这将打印结果 1
如果您在字典中有字典,请执行以下操作
dict = {1: {'a': 'abc', 'b': 'xyz'}}
print(dict[1][a])
这将打印 abc
将字典放入字典的另一种替代方法是让特定的键保存一个元组,请看以下示例
dict = {'a': ('abc', 'xyz'), 'b':('qwe', 12)}
for i in dict:
print(dict[i][1])
这将遍历您的字典并打印 xyz 和 12。您还可以给出一个条件,仅当它符合特定年龄时才打印它。
如果您愿意,您还可以遍历嵌套的字典。
根据您的需要,您可以让它包含字典或元组,但是,我认为元组使代码更易于阅读和理解(这是我的偏好)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.