python - Python从字典内的列表中获取值
问题描述
我有一本看起来像这样的字典:
{
'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))
以上产生以上...
选项 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))
上面产生了这个:
推荐阅读
- r - 合并R中的重复字符,同时保留数据框结构
- bash - 为什么 sed 命令会跳过 bash 中的某些行?
- python - 如何从 python pptx 生成的幻灯片中删除空白文本框?
- java - 使用 Java 的莱布尼茨公式
- java - 尝试运行我的 java 程序时出现错误:java.lang.ClassNotFoundException: jdk.tools.jaotc.Main
- python - 为什么 driver.find_element_by_xpath("//div[@class='_2ycp _5xhk']") 函数获取不同格式的数据?
- r - r curl_download 不喜欢文件名的地方
- corda - 从最后一个检查点错误重放流程
- bash - Bash自动完成以列出某个目录下的文件并传递给下一个命令
- bixbystudio - 尝试在 Mac 上的 Bixby Studio 上启用麦克风