Python从字典中的列表中获取值

时间:2019-09-03 02:18:54

标签: python python-3.x

我有一本这样的字典:

{
    'Video Content Started': [
        {
            'event_video_properties': ['first', 'first']
        },{
            'event_page_level_properties': ['second', 'second']
        }
    ],
    'Video Content Playing': [
        {
            'event_video_properties': ['third', 'third']
        },{
            'event_page_level_properties': ['fourth', 'fourth']
        }
    ]
}

我想获取所有值的列表(第一,第二,第三,第四)。

3 个答案:

答案 0 :(得分:0)

为您编写了一个lil函数,根据您的规范进行了自定义...

选项1

struct = {
    'Video Content Started': [
        {
            'event_video_properties': ['first', 'first']
        },{
            'event_page_level_properties': ['second', 'second']
        }
    ],
    'Video Content Playing': [
        {
            'event_video_properties': ['third', 'third']
        },{
            'event_page_level_properties': ['fourth', 'fourth']
        }
    ]
}

def getValues(struct):
    values = []
    for a in struct.keys():
        for b in struct[a]:
            for c in b:
                for d in range(0, len(b[c])):
                    values.append((b[c][d]))
    return values

print(getValues(struct))

上面产生了...

enter image description here

选项2

struct = {
    'Video Content Started': [
        {
            'event_video_properties': ['first', 'first']
        },{
            'event_page_level_properties': ['second', 'second']
        }
    ],
    'Video Content Playing': [
        {
            'event_video_properties': ['third', 'third']
        },{
            'event_page_level_properties': ['fourth', 'fourth']
        }
    ]
}

def getValues(struct):
    values = []
    for a in struct.keys():
        for b in struct[a]:
            for c in b:
                for d in range(0, len(b[c])):
                    if d == 1:  
                        values.append((b[c][d]))
    return values

print(getValues(struct))

上面产生此:

enter image description here

答案 1 :(得分:0)

data = {
  'Video Content Started': [{
    'event_video_properties': ['first', 'first']
  }, {
    'event_page_level_properties': ['second', 'second']
  }],
  'Video Content Playing': [{
    'event_video_properties': ['third', 'third']
  }, {
    'event_page_level_properties': ['fourth', 'fourth']
  }]
}

tem = [set(x) for _, v in data.items() for i in v for _, x in i.items()]
res = tuple()
for i in tem:
    res += tuple(i)
print(res) 
#('first', 'second', 'third', 'fourth')

答案 2 :(得分:0)

from itertools import chain


def foo(content):
    # Desired output
    output = set()

    # The values of at the highest level of the nested dictionary
    properties = chain.from_iterable(video_contents.values())

    # Iterate over the propertes
    for property in properties:
        # Convert list into tuple into set.
        # . This is because a list cannot be added to a set
        property_values = set(tuple(*property.values()))

        # Add values to the growing set
        output.add(*property_values)
    return output