python - 将列表变量名转换为键,将其值转换为python中字典的值
问题描述
我有一个 Python 列表,例如:
lst = ['a', 'b', 'c', 'd']
我想要 adictionary
和key
aslst
和value
a,b,c,d。
任何帮助,将不胜感激。提前致谢
解决方案
方法如下:
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'}
推荐阅读
- javascript - 提交表单时防止网页滚动到顶部
- node.js - 你如何用 Jest 模拟一个外部 npm 模块?
- django - django-webpush:无法在请求对象中找到用户
- php - 如何获取cpanel中文件夹内所有文件夹名称的列表?
- r - 使用“group_by”来运行分组线性回归是否足够?
- mysql - MySQL 排序让人头疼
- python - pandas groupby 然后合并为一行与新列
- material-ui - 如何从选择中获取名称和 ID
- html - 如何使用 JavaScript/JQuery 遍历选定的项目(语言)?
- javascript - 舍入数字同时支持负值