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

2024-09-30 10:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我们的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键和分子量作为一个字典的值?你知道吗


Tags: 代码inimportidfor字典序列result
2条回答

您正在创建一个字典,每个迭代有一个条目。你知道吗

你想:

  • 在循环之前定义一个dict变量(最好使用dct以避免重用内置类型名)
  • 将赋值重写为循环中的dict

所以在循环之前:

dct = {}

在循环中(而不是if+dict =代码),在三元表达式中,min&max只计算一次:

minval = min(molw)
maxval = max(molw)
dct[record.id] = molw if minval == maxval else (minval,maxval)

在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) 

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

相关问题 更多 >

    热门问题