Python 将列表添加到dict时输出错误

Python 将列表添加到dict时输出错误,python,numpy,Python,Numpy,我试图添加列表l作为不同dictd键的值。对于具有[6,12,18,24,30]的数组a,我试图使dictd包含以下键值对: d[6] = [0, 0, 0.....0] d[12] = [6, 0, 0, ..0] d[18] = [6, 12, 0, ...0] d[24] = [6, 12, 18, 0, ..0] 其中,上述每个列表中有59个元素 我使用下面的代码来执行此操作,但我对键24的输出是: {24: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

我试图添加列表
l
作为不同dict
d
键的值。对于具有
[6,12,18,24,30]
的数组
a
,我试图使dict
d
包含以下键值对:

d[6] = [0, 0, 0.....0]
d[12] = [6, 0, 0, ..0]
d[18] = [6, 12, 0, ...0]
d[24] = [6, 12, 18, 0, ..0]
其中,上述每个列表中有59个元素

我使用下面的代码来执行此操作,但我对键
24
的输出是:

{24: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 12, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 18, 12, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}
我正在努力理解我错在哪里

d = {}
l = []
a =numpy.array([6,12,18,24,30])
for x, value in numpy.ndenumerate(a): 
    months_to_maturity = value
    for i in range(6, 354, 6):
        if i <= months_to_maturity:
            l.append(months_to_maturity - i)
        else:
            l.append(0)

    d[months_to_maturity] = l
d={}
l=[]
a=numpy.数组([6,12,18,24,30])
对于x,数值单位为numpy.ndenumerate(a):
月至到期日=价值
对于范围(6354,6)内的i:

如果我你总是附加到同一个列表中。因此,所有字典值最终都指向同一个列表。每次都要附加到不同的列表:

d = {}
a = numpy.array([6, 12, 18, 24, 30])
for months_to_maturity in a:
    l = []
    for i in range(6, 354, 6):
        if i <= months_to_maturity:
            l.append(months_to_maturity - i)
        else:
            l.append(0)

    d[months_to_maturity] = l
d={}
a=numpy.array([6,12,18,24,30])
在一个月到一个月的期限内:
l=[]
对于范围(6354,6)内的i:

如果我你总是附加到同一个列表中。因此,所有字典值最终都指向同一个列表。每次都要附加到不同的列表:

d = {}
a = numpy.array([6, 12, 18, 24, 30])
for months_to_maturity in a:
    l = []
    for i in range(6, 354, 6):
        if i <= months_to_maturity:
            l.append(months_to_maturity - i)
        else:
            l.append(0)

    d[months_to_maturity] = l
d={}
a=numpy.array([6,12,18,24,30])
在一个月到一个月的期限内:
l=[]
对于范围(6354,6)内的i:

如果我是你得到一个错误?与实际产出相比,你的预期产出是多少?@Claris我在问题中写道,我得到了什么样的产出,我想要什么。我说过我得到了错误的输出,但没有提到得到了错误。你得到了错误吗?与实际产出相比,你的预期产出是多少?@Claris我在问题中写道,我得到了什么样的产出,我想要什么。我说过我得到了错误的输出,但没有提到得到错误。