python循环字典值引用更新所有值

时间:2013-02-11 02:47:48

标签: python variables dictionary nested

我在python中更新字典中的值时遇到问题。我正在尝试为单个第一级键更新嵌套值(作为int或列表),而是更新所有第一级键的值。

我首先创建字典:

kmerdict = {}
innerdict = {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 'lowstart':0,'totaluncover':0, 'totalbases':0}
for kmer in kmerlist: # build kmerdict
    kmerdict [kmer] = {}
    for chrom in fas:  #open file and read line
        chromnum = chrom[3:-3]
        kmerdict [kmer][chromnum] = innerdict

然后我从列表(fas,未显示)中走过染色体(作为纯文本文件),并以7mer字符串(k = 7)作为关键字。如果该键位于我正在寻找的键列表中(kmerlist)并尝试使用它来引用嵌套在字典中的单个值:

for chrom in fas:  #open file and read line
    chromnum = chrom[3:-3]
    p = 0  #chromosome position counter
    thisfile = "/var/store/fa/" + chrom
    thischrom = open(thisfile)
    thischrom.readline()
    thisline = thischrom.readline()
    thisline = string.strip(thisline.lower())
    l=0  #line counter
    workline = thisline
    while(thisline):
        if len(workline) > k-1:
            thiskmer = ''
            thiskmer = workline[0:k] #read five bases
            if thiskmer in kmerlist:
                thisuncovered = kmerdict[thiskmer][chromnum]['uncovered']
                thisendcover =  kmerdict[thiskmer][chromnum]['endcover']
                thiscoverholder = kmerdict[thiskmer][chromnum]['coverholder']
                if p >= thisendcover:
                    thisuncovered += (p - thisendcover)
                    thisendcover = ((p+k) + ext)
                    thiscoverholder.append(p)
                elif p < thisendcover:
                    thisendcover = ((p+k) + ext)
                    thiscoverholder.append(p)
            print kmerdict[thiskmer]
            p += 1
            workline = workline[1:]
        else:
            thisline = thischrom.readline()
            thisline = string.strip(thisline.lower())
            workline = workline+thisline
            l+=1
print kmerdict

但是当我打印字典时,所有“thiskmer”级别都会使用相同的值进行更新。我对字典不是很好,我看不出我的方式的错误,但它们是深刻的!谁能开导我?

希望我已经足够清楚了。我现在一直在修补这段代码:(

1 个答案:

答案 0 :(得分:1)

坦白 - 我没有花时间弄清楚你的所有代码 - 只有第一部分。您遇到的第一个问题是设置:

kmerdict = {}
innerdict = {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 
             'lowstart':0,'totaluncover':0, 'totalbases':0}
for kmer in kmerlist: # build kmerdict
    kmerdict [kmer] = {}
    for chrom in fas:  #open file and read line
        chromnum = chrom[3:-3]
        kmerdict [kmer][chromnum] = innerdict

您创建innerdict一次,然后重新使用相同的字典。换句话说,每个kmerdict[kmer][chromnum]指的是相同的对象。也许将最后一行更改为:

kmerdict [kmer][chromnum] = copy.deepcopy(innerdict)

会有所帮助(在文件顶部适当导入copy)?或者,您可以将innerdict的创建移动到内部循环中,如注释中所指出的那样:

def get_inner_dict():
    return {'endcover':0, 'coverdict':{}, 'coverholder':[], 'uncovered':0, 
            'lowstart':0,'totaluncover':0, 'totalbases':0}

kmerdict = {}
for kmer in kmerlist: # build kmerdict
    kmerdict [kmer] = {}
    for chrom in fas:  #open file and read line
        chromnum = chrom[3:-3]
        kmerdict [kmer][chromnum] = get_inner_dict()

- 我决定使用一个函数来更容易阅读:)。