【发布时间】:2021-06-17 15:43:13
【问题描述】:
我有一个 Python 列表,例如:
lst = ['a', 'b', 'c', 'd']
我想要一个dictionary,其中key 为lst,value 为a、b、c、d。
任何帮助将不胜感激。提前致谢
【问题讨论】:
-
dictionary = {'lst': lst}?
标签: python list dictionary type-conversion
我有一个 Python 列表,例如:
lst = ['a', 'b', 'c', 'd']
我想要一个dictionary,其中key 为lst,value 为a、b、c、d。
任何帮助将不胜感激。提前致谢
【问题讨论】:
dictionary = {'lst': lst}?
标签: python list dictionary type-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'}
【讨论】: