Python:矩阵字典为文件中的每个键保存相同的值

时间:2015-10-30 08:52:26

标签: python-2.7 dictionary matrix matplotlib

我想创建一个函数,它接受一个数据文件和一个字典,其中每个值都是nympy nan的(???,12)矩阵,并返回带有数据文件值的字典。并非该文件中的所有数据都应该进入字典。

我设法获得所有正确的数据并将其放入第一个矩阵,事情是每个矩阵都是第一个的副本!我不明白为什么会这样,因为我会逐行获取所有数据点。

def read_temperature_dict(datfile, tempdictionary):
    ''''something'''
    dat = open(datfile)
    for key in tempdictionary:
        for line in dat.readlines():
            ID = line[:11]
            lineyear = line[11:15]
            lineyear = int(lineyear)
            try:
                indexlineyear = year_to_index(lineyear)
            except:
                continue
            jan = line[19:24]
            feb = line[27:32]
            mar = line[35:40]
            apr = line[43:48]
            may = line[51:56]
            jun = line[59:64]
            jul = line[67:72]
            aug = line[75:80]
            sep = line[83:88]
            oct = line[91:96]
            nov = line[99:104]
            dec = line[107:112]
            months = [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
            count = 0
            for temp in months:
                newtemp = float(temp)
                if count <= 12:
                    if newtemp == -9999:
                        count += 1
                        continue
                    else:
                        newtemp = newtemp / 100.0
                        tempdictionary[ID][indexlineyear, count] = newtemp
                        count += 1
    dat.close()
    return tempdictionary 

所以,如果我写:

mydicttwo = read_temperature_dict('ghcnm.tavg.v3.3.0.20151011.qca_test.dat', mydict)  
for key in mydicttwo:  
    print key, mydicttwo[key]  

结果是两个相同的键:

20558362000 [[ 2.56 4.86 8.46 ..., 18.46 12.36 7.16] [ 5.26 1.76 8.26 ..., 18.66 11.26 5.96] [ 6.66 5.66 11.06 ..., 18.36 14.16 7.46] ..., [ 2.2 2. 6.3 ..., 17.6 9.9 5.6 ] [ 2.9 3.1 6.3 ..., 15.8 11.2 4.2 ] [ 3.4 4.1 6.3 ..., nan nan nan]]  
21047600000 [[ 2.56 4.86 8.46 ..., 18.46 12.36 7.16] [ 5.26 1.76 8.26 ..., 18.66 11.26 5.96] [ 6.66 5.66 11.06 ..., 18.36 14.16 7.46] ..., [ 2.2 2. 6.3 ..., 17.6 9.9 5.6 ] [ 2.9 3.1 6.3 ..., 15.8 11.2 4.2 ] [ 3.4 4.1 6.3 ..., nan nan nan]]

0 个答案:

没有答案