【发布时间】:2020-06-09 04:25:18
【问题描述】:
很难描述我的问题,所以让我列出场景和我有什么:
listofkeys=[]
listofvalues=[]
for it in practice_list:
it1= it[0:5]
it2= it[8:19]
listofkeys.append(it1)
listofvalues.append(it2)
di = dict(zip(listofkeys, listofvalues))
出于安全原因,这会打印一些我无法显示的内容,但是对于 practice_list 中的原始字符串,有些名称会重复并且具有不同的值,例如:['apple is green','apple is red','banana是黄色的'...]。当我通过索引将这个列表与 listofnames 和 listofcolors 分开然后压缩到字典中时,它基本上输出:{'apple':'red','banana':yellow'...}。 for 循环似乎正在覆盖另一个值,对吧?我怎样才能阻止这种情况并让字典打印 {'apple': 'green', 'red', 'banana':yellow'...}
【问题讨论】:
-
每个键只能有一个值,如果你想要多个项目,当然可以是一个列表。
-
这能回答你的问题吗? Appending values of dict list
-
dict对象是映射,它们将 single 可散列对象映射到 single 其他对象。当然,该值可以是某种形式的容器,例如列表。
标签: python dictionary