【问题标题】:Loop through dictionary to match dictionary key to a list of values and append dictionary遍历字典以将字典键与值列表匹配并附加字典
【发布时间】:2018-08-23 16:27:13
【问题描述】:

我有两个使用 biopython 创建的字典(dict_a 和 dict_b)来解析我的 fasta 文件。如果在 dict_b 键中找到 dict_a 键(它们是基因名称),我想在 dict_a 中附加来自 dict_b 的匹配键(基因名称)的值(并非 dict_a 中的所有键都在 dict_b 中)。

到目前为止,我已经创建了两个字典和一个来自 dict_a 键 (list_a) 的基因名称列表。

如果 dict_a 中的键存在于 dict_b 中,我需要将 dict_a 的值附加到 dict_b 中。 我需要帮助循环遍历 dict_b 以查找与 dict_a 键(我放入 list_a)的匹配项然后附加dict_a。

提前感谢您的帮助!如果我不够清楚,请告诉我。

dict_a = SeqIO.to_dict(SeqIO.parse(dict_a_path, "fasta"))
dict_b = SeqIO.to_dict(SeqIO.parse(dict_b_path, "fasta"))

list_a = list(dict_a.keys())

【问题讨论】:

    标签: python-3.x dictionary merge python-requests biopython


    【解决方案1】:

    如果dict_a 中的键存在于dict_b 中,我需要将dict_b 的值附加到dict_a

    您可以遍历dict 的键,并分别使用forin 检查一个键是否在另一个dict 中:

    for key in dict_a:
        if key in dict_b:
            dict_a[key].append(dict_b[key])
    

    【讨论】:

    • 谢谢!我试过了,但它不起作用。我收到一条错误消息“AttributeError:'SeqRecord' 对象没有属性'append'”。我认为这是因为我的字典是使用 biopython 从 fasta 文件创建的。
    • @jws 不幸的是,我不了解 biopython,无法进一步帮助您。阅读文档,您似乎无法改变 seqRecord,但我很可能会弄错。
    • 感谢您的帮助!我想我使用了 dict_a[key].append.features(dict_b[key]) 来合并字典并将它们写到一个文件中,我现在只是想知道如何拆分这些行。再次感谢!
    • @jws 很高兴您能解决您的问题。我鼓励您将修复它的内容发布为您自己问题的答案。这很可能在未来对其他人有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2020-11-04
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多