gpt4 book ai didi

Python 嵌套字典和列表将字典键与列表值连接起来

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:38 24 4
gpt4 key购买 nike

我有一个嵌套的字典如下:

list = { "A" : [ "1", "2", "3" ], "B" :  [ "2" ], "C" : [ "1", "2" ] }

我想按如下方式连接字典键和列表值:

[ "A.1", "A.2", "A.3", "B.2", "C.1", "C.2" ]

有什么建议吗?

最佳答案

你可以试试下面的代码:

list = { "A" : [ "1", "2", "3" ], "B" :  [ "2" ], "C" : [ "1", "2" ] }
data = []
for key, values in list.items():
for value in values:
data.append("{0}.{1}".format(key,value))
print(data)
# Output ['A.1', 'A.2', 'A.3', 'B.2', 'C.1', 'C.2']

关于Python 嵌套字典和列表将字典键与列表值连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50519063/

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