gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:56:28 24 4
gpt4 key购买 nike

我有一本看起来像这样的字典:

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

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

最佳答案

为您编写了一个 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

关于Python从字典中的列表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57764297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com