gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:49 25 4
gpt4 key购买 nike

我编写了一个小的 Python 程序来遍历数据文件 (input_file) 并执行计算。如果计算结果达到某些状态(stateAstateB),则从结果中提取信息(hits)。要提取的匹配项取决于三个参数集中的参数。
我使用字典字典来存储我的参数集 (param_sets),并使用列表字典来存储命中 (hits)。字典 param_setshits 具有相同的键。

问题是,

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

最佳答案

你的线路:

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] = []

也就是说,一个新的和单独的列表对象作为每个条目的值。等价地,

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

顺便说一句,当您确实需要制作容器的(浅)副本时,最通用的方法通常是调用容器的类型,并将旧容器作为参数,如下所示:

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

等等;这也适用于在类型之间转换容器(newlist = list(oldset) 从集合中创建列表,等等)。

关于python:列表字典以某种方式耦合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1341208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com