首页 > 解决方案 > 将列表变量名转换为键,将其值转换为python中字典的值

问题描述

我有一个 Python 列表,例如:

lst = ['a', 'b', 'c', 'd']

我想要 adictionarykeyaslstvaluea,b,c,d。

任何帮助,将不胜感激。提前致谢

标签: pythonlistdictionarytype-conversion

解决方案


方法如下:

lst = ['a', 'b', 'c', 'd']
dct = {'lst': lst}

print(dct)

输出:

{'lst': ['a', 'b', 'c', 'd']}

但是,如果您期望的值真的是,则a,b,c,d需要使用str.join()列表中的方法:

lst = ['a', 'b', 'c', 'd']
dct = {'lst': ','.join(lst)}

print(dct)

输出:

{'lst': 'a,b,c,d'}

推荐阅读