gpt4 book ai didi

python - 包含来自多个列表的数据的字典结构

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:06 31 4
gpt4 key购买 nike

我想请求有关以下代码结构的小指导。

例如,假设我已经创建了以下列表并填充了数据:

movie_name
movie_description
movie_poster
movie_year
movie_duration

是否可以使用从列表中获取的数据制作具有以下结构的字典:

    { movie_name : [
{description : movie_description},
{poster : movie_poster},
{year : movie_year},
{duration : movie_duration}
]
}

谢谢。

最佳答案

如果这些列表的索引对齐,您只需将它们压缩在一起即可获取包含这些值的元组。然后,您可以通过简单的字典理解将其转换为您想要的数据结构。

res = {
name: {
'description': description,
'poster': poster,
'year': year,
'duration': duration
}
for name, description, poster, year, duration in zip(
movie_name,
movie_description,
movie_poster,
movie_year,
movie_duration
)
}

关于python - 包含来自多个列表的数据的字典结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895657/

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