python - 如何将函数的单独字典输出合并到一个字典中?

标签 python dictionary

对于我们的 python 项目,我们必须解决多个问题。然而,我们陷入了这一困境:

“编写一个函数,给定 FASTA 文件名,返回一个字典,其中序列 ID 作为键,元组作为值。该值表示序列的最小和最大分子量(序列可能不明确)。 ”

import collections
    from Bio import Seq
    from itertools import product
    def ListMW(file_name):
        seq_records = SeqIO.parse(file_name, 'fasta',alphabet=generic_dna)
        for record in seq_records:
            dictionary = Seq.IUPAC.IUPACData.ambiguous_dna_values
            result = []
            for i in product(*[dictionary[j] for j in record]):
                result.append("".join(i))
                molw = []
            for sequence in result:
                molw.append(SeqUtils.molecular_weight(sequence))
            tuple= (min(molw),max(molw))
            if min(molw)==max(molw):
                dict={record.id:molw}
            else:
                dict={record.id:(min(molw), max(molw))}

            print(dict) 

使用此代码,我们设法获得以下输出:

{'seq_7009': (6236.9764, 6367.049999999999)}
{'seq_418': (3716.3642000000004, 3796.4124000000006)}
{'seq_9143_unamb': [4631.958999999999]}
{'seq_2888': (5219.3359, 5365.4089)}
{'seq_1101': (4287.7417, 4422.8254)}
{'seq_107': (5825.695099999999, 5972.8073)}
{'seq_6946': (5179.3118, 5364.420900000001)}
{'seq_6162': (5531.503199999999, 5645.577399999999)}
{'seq_504': (4556.920899999999, 4631.959)}
{'seq_3535': (3396.1715999999997, 3446.1969999999997)}
{'seq_4077': (4551.9108, 4754.0073)}
{'seq_1626_unamb': [3724.3894999999998]}

正如你所看到的,这不是一本字典,而是多本字典。那么我们是否可以更改代码或输入额外的命令来获取这种格式:

{'seq_7009': (6236.9764, 6367.049999999999),
'seq_418': (3716.3642000000004, 3796.4124000000006),
'seq_9143_unamb': (4631.958999999999),
'seq_2888': (5219.3359, 5365.4089),
'seq_1101': (4287.7417, 4422.8254),
'seq_107': (5825.695099999999, 5972.8073),
'seq_6946': (5179.3118, 5364.420900000001),
'seq_6162': (5531.503199999999, 5645.577399999999),
'seq_504': (4556.920899999999, 4631.959),
'seq_3535': (3396.1715999999997, 3446.1969999999997),
'seq_4077': (4551.9108, 4754.0073),
'seq_1626_unamb': (3724.3894999999998)}

或者以某种方式设法明确它应该使用 seq_ID ans 键和分子量作为一个字典的值?

最佳答案

在 for 循环之前设置一个字典,然后在循环期间更新它,例如:

import collections
    from Bio import Seq
    from itertools import product
    def ListMW(file_name):
        seq_records = SeqIO.parse(file_name, 'fasta',alphabet=generic_dna)
        retDict = {}
        for record in seq_records:
            dictionary = Seq.IUPAC.IUPACData.ambiguous_dna_values
            result = []
            for i in product(*[dictionary[j] for j in record]):
                result.append("".join(i))
                molw = []
            for sequence in result:
                molw.append(SeqUtils.molecular_weight(sequence))
            tuple= (min(molw),max(molw))
            if min(molw)==max(molw):
                retDict[record.id] = molw
            else:
                retDict[record.id] = (min(molw), max(molw))}
            # instead of printing now, print in the end of your function / script
            # print(dict) 

现在,您在循环的每一轮设置一个新字典,并打印它。打印大量字典只是代码的正常行为。

关于python - 如何将函数的单独字典输出合并到一个字典中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47908310/

相关文章:

python - BeautifulSoup Python 脚本不再适用于挖掘简单的字段

python beautifulsoup 字典表与列表

java - 将 HashMap 添加到 TreeSet 的奇怪行为

python - 安装到(非 root)用户帐户后如何找到 python 命令行工具?

python - 如何在Python中找到矩阵最大数的索引?

python - 如何在 Perl 中实现多键哈希作为 Python 中的嵌套字典?

Python:从value中获取对应的key

java - 在java中获取hashmap中的最高值

python - 在 Raspberry 上使用 python 读取原始以太网数据包

python - 替换 DataFrame 中的值