【问题标题】:How can I combine separate dictionary outputs from a function in one dictionary?如何在一个字典中组合来自函数的单独字典输出?
【发布时间】:2017-12-20 14:32:53
【问题描述】:

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

【问题讨论】:

  • 您可能需要使用update

标签: python dictionary


【解决方案1】:

在 for 循环之前设置 dictionnary,然后在循环期间更新它,例如:

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 只是代码的正常行为。

【讨论】:

  • 感谢您的成功!我们几乎得到了我们需要的输出:)
  • 不客气,但@Jean-François Fabre 也值得您的支持。他还提示了如何优化您的代码。
  • 是的,确实,我正在尝试喜欢它和你的,但是我不能,因为我是新用户...
【解决方案2】:

您正在创建一个字典,每次迭代都有 1 个条目。

你想:

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

所以在循环之前:

dct = {}

在循环中(而不是你的if + dict = 代码),在一个三元表达式中,最小值和最大值只计算一次:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多