【发布时间】:2022-11-17 18:49:24
【问题描述】:
我正在尝试从字符串列表创建嵌套字典。 字符串的每个索引对应一个键,而每个字符对应一个值。
我有一个清单:
list = ['game', 'club', 'party', 'play']
我想创建一个(嵌套的)字典:
dict = {0: {'g', 'c', 'p', 'p'}, 1: {'a', 'l', 'a', 'l'}, 2: {'m', 'u', 'r', 'a'}, etc.}
我在想一些事情:
res = {}
for item in range(len(list)):
for i in list[item]:
if i not in res:
# create a key (index - ex. '0') and a value (character - ex. 'g' of 'game')
else:
# put the value in the corresponding key (ex. 'c' of 'club')
print(res)
【问题讨论】:
-
你真的想要
sets 作为你的键值吗?
标签: python string list dictionary