将列表与字典中的键进行比较,如果我来自列表 = 字典打印值

Compare list with key from dict, if i from list = dict print value

给出一个字典 (generate_translation_dict),其中三元组作为键,氨基酸作为值,我想将三元组列表与该字典中的现有键进行比较。如果它们彼此相等,它应该打印值。

例如:

generate_translation_dict = {'ATA': 'I',
  'ATC': 'I',
  'ATT': 'I',
  'ATG': 'M',
}
comparison_list = [ 
  'ATG',
  'TTT',
  'GTT',
]

如果列表中的一项,例如 'ATG',与字典中的键相同,那么它应该打印 'M'。比较列表是从文本文件导入的,包含约 1200 项。

到目前为止我的代码:

triplets = [] #This block imports the tripplets
with open('triplets.txt', 'r') as infile:
    for line in infile:
        line = line.strip('\n') # um \n zu entfernen
        triplets.append(line)

aa = [] # aa for amino acids
with open('amino_acids.txt', 'r') as infile:
    for line in infile:
        line = line.strip('\n') # um \n zu entfernen
        aa.append(line)

generate_translation_dict = {} # empty dictionary
for n in range(len(triplets)): 
    generate_translation_dict[triplets[n]] = aa[n] # adding elements to the dict # triplets added as keys/ aa added as value
print(generate_translation_dict)

tripletsequence = []
with open('triplet_sequence.txt', 'r') as infile:
    for line in infile:
        line = line.strip('\n') # um \n zu entfernen
        tripletsequence.append(line)

如你所见,三联体序列就是列表。比较应该是一个循环,直到列表完成。

获得翻译字典后,您需要做的就是遍历 triplet_sequence 并将三元组用作字典中的键。

您还可以通过 zipping 两个文件流并将结果放入字典理解中来大大简化翻译字典的创建:

with open('triplets.txt') as triplets, open('amino_acids.txt') as aa:
    translation = {
        t.strip(): a.strip() 
        for t, a in zip(triplets, aa)
    }
with open('triplet_sequence.txt') as f:
    triplet_sequence = [
        translation[t.strip()]
        for t in f
    ]

print("\n".join(triplet_sequence))
% cat triplets.txt
ATA
ATC
ATT
ATG

% cat amino_acids.txt
I
I
I
M

% cat triplet_sequence.txt
ATG
ATT
ATC

% python test.py
M
I
I

一旦你的翻译指令就位(请检查最后的建议):

translation_dict = {
                    'ATA': 'I',
                    'ATC': 'I',
                    'ATT': 'I',
                    'ATG': 'M',
                    'TTT': 'P',
                    'GTT': 'V'
                  }

密码子列表:

comparison_list = [ 
                    'ATG',
                    'TTT',
                    'GTT',
                  ]

你可以这样解码氨基酸序列:

seq = [translation_dict[k] for k in comparison_list]
print(seq) #=> ['M', 'P', 'V']

使用 get() 方法可以避免丢失密钥的错误,如果你觉得这是个好主意(returns None 作为丢失密钥的元素):

seq = [translation_dict.get(k) for k in comparison_list]

或者,删除 None 个元素:

seq = list(filter(None, [translation_dict.get(k) for k in comparison_list]))

我建议将翻译字典存储为 JSON 文件 以便直接加载为字典,以备使用。