Python如何从特定类型的列表中获取价值

时间:2020-08-24 07:10:06

标签: python python-3.x

我想打印entity_id类型的attributeString值,例如

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'}]

ngsi_type = ( 'Array', 'Boolean')
    

这是代码的实际定义,我要在其中打印id and attribute的值,即temperature , pressure的{​​{1}}而不是{{1}的type }}我希望将其ngsi_type的值打印出来

type : String

关于它的任何帮助,因为我是python的新手,除了使用msg的ngsi类型外,无法打印entity_id and attribute的值

2 个答案:

答案 0 :(得分:1)

您在遍历列表的方向上是正确的,但是您需要遍历每个Dictionary以查找您的类型。

要遍历每个键值对:

for key, value in e.items():

然后要检查嵌套字典的类型,您首先要检查它,确保键存在,最后确保它不是NSGI_TYPE类型。看起来像这样:

for e in entities:
    entity_id = ""
    # put entity_id into scope for our loop

    for key, value in e.items():
    # loop over the { ... }

        if key == "id":
            entity_id = str(value)
        # keep track of id to output later

        if type(value) is dict:
            if "type" in value.keys():
            # if type isn't in the { ... } there's no point in continuing
                if value["type"] != NSGI_TYPE:
                    print(entity_id)
                    print(key, value)
                else:
                    msg = "Entity {} is type {}."
                    raise ValueError(msg.format(e[NGSI_ID], entity_type))

答案 1 :(得分:1)

您可以使用isinstance(obj, type)检查属性本身是否为字典-如果需要,则需要“下降”到该属性。您可以像这样压扁您的字典:

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': 'Room not shown due to type',
             'temperature': {'value': '10', 'type': 'Number'},
             'pressure':    {'value': '12', 'type': 'Number'}, 
             'type': 'Array',
             'time_index': '2020-08-24T06:23:37.346664'}]

ngsi_type = ('Array', 'Boolean')

for ent in entities:
    if ent["type"] in ngsi_type:
        continue   # do not show any entities that are of type Array/Boolean
    id = ent["id"]
    for key in ent:  # check each key of the inner dict 
        if key == "id":
            continue # do not print the id

        # if dict, extract the value and print a message, else just print it
        if isinstance(ent[key], dict):
            print(f"id: {id} - attribute {key} has a value of {ent[key]['value']}")
        else:
            # remove if only interested in attribute/inner dict content
            print(f"id: {id} - attribute {key} = {ent[key]}")
    print()

输出:

id: Room1 - attribute temperature has a value of 10
id: Room1 - attribute pressure has a value of 12
id: Room1 - attribute type = Room
id: Room1 - attribute time_index = 2020-08-24T06:23:37.346348

id: Room2 - attribute temperature has a value of 10
id: Room2 - attribute pressure has a value of 12
id: Room2 - attribute type = Room
id: Room2 - attribute time_index = 2020-08-24T06:23:37.346664