向 python 中的键添加第二个值

Add a second value to a key in python

我需要将第二个值添加到来自另一个列表的字典中的键。有人可以解释一下怎么做吗?

这就是我想要的,但我总是收到错误任何想法?

third_value_list =[0] 
for i in third_value_list 
num_list = [1,2] 
val_list = [0,1] 
dict1 = dict((k, [v]+i) for (k, v) in zip(num_list,val_list)) 
print dict1 

我想使用循环遍历列表,但我不断收到错误

我想要的期望输出是关键:x,y 下面:

{1: [0,0], 2:[1,0]}

这种方法怎么样:

third_value_list =[0]
num_list = [1,2]
val_list = [0,1]
# If you want it as a tuple, then just use tuple([v]+third_value_list) below
dict1 = dict((k, [v]+third_value_list) for (k, v) in zip(num_list,val_list))
print dict1

这将打印

{1: [0, 0], 2: [1, 0]} # Tuple variant will print {1: (0, 0), 2: (1, 0)}

备注

您不能在 python 中这样定义字典:

>>> {1: 2,3, 2:1,0}
  File "<pyshell#61>", line 1
    {1: 2,3, 2:1,0}
           ^
SyntaxError: invalid syntax

列表、元组和其他集合可以正常工作:

>>> {1: (2,3), 2:(1,0)}
{1: (2, 3), 2: (1, 0)}
>>> {1: [2,3], 2:[1,0]}
{1: [2, 3], 2: [1, 0]}

编辑您的最新评论(见下文):

>>> d = {1: 0}
>>> l = [1, 2, 3]
>>> for k, v in d.iteritems():
...     d[k] = tuple([v]+l)
...     
>>> d
{1: (0, 1, 2, 3)}
>>>