python:列表的字典以某种方式耦合

时间:2009-08-27 13:45:37

标签: python variables dictionary

我编写了一个小的python程序来迭代数据文件( input_file )并执行计算。如果计算结果达到某些状态( stateA stateB ),则会从结果中提取信息( hits )。提取的命中取决于三个参数集的参数 我使用字典词典来存储我的参数集( param_sets )和列表来存储命中( hits )。字典 param_sets hits 具有相同的键。

问题是,

hits 字典中的列表以某种方式耦合。当一个列表更改时(通过调用 extract_hits 函数),其他列表也会更改。

这里是(缩短的)代码:

import os, sys, csv, pdb
from operator import itemgetter

# define three parameter sets
param_sets = {
    'A' : {'MIN_LEN' : 8, 'MAX_X' : 0, 'MAX_Z' : 0},
    'B' : {'MIN_LEN' : 8, 'MAX_X' : 1, 'MAX_Z' : 5},
    'C' : {'MIN_LEN' : 9, 'MAX_X' : 1, 'MAX_Z' : 5}}

# to store hits corresponding to each parameter set
hits = dict.fromkeys(param_sets, [])

# calculations
result = []
for input_values in input_file:
    # do some calculations
    result = do_some_calculations(result, input_values)
    if result == stateA:
        for key in param_sets.keys():
            hits[key] = extract_hits(key, result,
                                                hits[key],
                                                param_sets[key]['MIN_LEN'],
                                                param_sets[key]['MAX_X'],
                                                param_sets[key]['MAX_Z'])
        result = []  # discard results, start empty result list
    elif result == stateB:
        for key in param_sets.keys():
            local_heli[key] = extract_hits(key,
                                           result,
                                           hits[key],
                                           param_sets[key]['MIN_LEN'],
                                           param_sets[key]['MAX_X'],
                                           param_sets[key]['MAX_Z'])
        result = [] # discard results
        result = some_calculation(input_values) # start new result list
    else:
        result = some_other_calculation(result) # append result list



def extract_hits(k, seq, hits, min_len, max_au, max_gu):
    max_len = len(seq)
    for sub_seq_size in reversed(range(min_len, max_len+1)):
        for start_pos in range(0,(max_len-sub_seq_size+1)):
            from_inc = start_pos
            to_exc = start_pos + sub_seq_size
            sub_seq = seq[from_inc:to_exc]
            # complete information about helical fragment sub_seq
            helical_fragment = get_helix_data(sub_seq, max_au, max_gu)
            if helical_fragment:
                hits.append(helical_fragment)
                # search seq regions left and right from sub_seq for further hits
                left_seq = seq[0:from_inc]
                right_seq = seq[to_exc:max_len]
                if len(left_seq) >= min_len:
                    hits = sub_check_helical(left_seq, hits, min_len, max_au, max_gu)
                if len(right_seq) >= min_len:
                    hits = sub_check_helical(right_seq, hits, min_len, max_au, max_gu)
                print 'key', k                 # just for testing purpose
                print 'new', hits              # just for testing purpose
                print 'frag', helical_fragment # just for testing purpose
                pdb.set_trace()                # just for testing purpose
                return hits # appended
    return hits # unchanged

这里是python调试器的一些输出:

key A
new ['x', 'x', 'x', {'y': 'GGCCGGGCUUGGU'}]
frag {'y': 'GGCCGGGCUUGGU'}
> 
-> return hits
(Pdb) c
key B

new [{'y': 'GGCCGGGCUUGGU'}, {'y': 'CCGGCCCGAGCCG'}]
frag {'y': 'CCGGCCCGAGCCG'}
> extract_hits()
-> return hits
(Pdb) c
key C
new [{'y': 'GGCCGGGCUUGGU'}, {'y': 'CCGGCCCGAGCCG'}, {'y': 'CCGGCCCG'}]
frag {'y': 'CCGGCCCG'}
> extract_hits()
-> return hits

键A 中的元素不应出现在键B 中,而键A和键B中的元素不应出现在键C 。

2 个答案:

答案 0 :(得分:8)

你的专栏:

hits = dict.fromkeys(param_sets, [])

相当于:

hits = dict()
onelist = []
for k in param_sets:
    hits[k] = onelist

也就是说,hits中的每个条目都具有SAME列表对象的值,最初为空,无论它具有什么密钥。请记住,赋值不执行隐式副本:而是指定“对RHS对象的另一个引用”。

你想要的是:

hits = dict()
for k in param_sets:
    hits[k] = []

即一个NEW AND SEPARATE列表对象作为每个条目的值。等价地,

hits = dict((k, []) for k in param_sets)
顺便说一句,当你需要制作容器的(浅)副本时,最通用的方法通常是调用容器的类型,以旧容器作为参数,如:

newdict = dict(olddict)
newlist = list(oldlist)
newset = set(oldset)

等等;这也可以在类型之间转换容器(newlist = list(oldset)从列表中创建一个列表,等等。)

答案 1 :(得分:4)

默认情况下,字典和列表通过引用传递。对于字典,而不是:

hits_old = hits      # just for testing purpose

它将是:

hits_old = hits.copy()      # just for testing purpose

这将复制字典的键/值配对,从而产生一个等效字典,不会包含对hits字典的未来更改。

当然,第二个函数中的hits_old实际上是一个列表,而不是一个字典,所以你想做一些类似于下面的事情来复制它:

hits_old = hits[:]

我不清楚为什么列表也没有copy()函数,万一你想知道。