[英]create a dictionary in python with a key and a list of values in python
I have the dictionary我有字典
list = ["1", "2", "3"]
and a single key.和一把钥匙。 now i want to form a dictionary like:
现在我想形成一个字典,如:
dic = {"key": "1", "key": "2", "key": "3"}
Does anyone know how to do it?有谁知道该怎么做?
I have tried:我努力了:
list = ["1", "2", "3"]
dic = {}
for value in list:
dic['key'] = value
print(dic)
But it returns:但它返回:
{'key': '3'}
thanks, Marius.谢谢,马吕斯。
Dictionary, by definition , can't have the same key twice.字典, 根据定义,不能有两次相同的键。
Consider storing the values under the same key, using other kind of container, such as list, set or tuple:考虑将值存储在同一个键下,使用其他类型的容器,例如列表、集合或元组:
{"key": ["1", "2", "3"]}
It's not possible, Python does not allow duplicate keys inside a dictionary.这是不可能的,Python 不允许字典中有重复的键。 Keys act as unique identifiers thus Duplicate keys might lead to ambiguity.
密钥充当唯一标识符,因此重复的密钥可能会导致歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.