使用字典转换字母

编程入门 行业动态 更新时间:2024-10-24 10:18:27
本文介绍了使用字典转换字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想写一个程序,将字典中匹配字符的字符转换为与该字符相关联的值。如果字典是{'A':'T','C':'G','T':'A','G':'C'},字符串是'AAG',输出应该是'TTC '。

编辑:这是我现在感谢你的一些答案:

def matching_codons(complementments,poolA): answer = [] codon ='' counter = 0 for i in poolA: for a in i: codon + = complements [a] counter + = 1 if counter == 3: answer.append(codon)

不幸的是,这只能翻译前3个字母 - 如何让它继续运行循环? p>

注意:poolA是字符串列表,例如['AAG','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT']

注意2:我不能硬编码任何像翻译表,因为技术上可以更改字典输入

解决方案

另一种解决方案是使用字符串中的maketrans

from string import maketrans complementTrans = maketrans(ACTG,TGAC) poolA = ['AAG ','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT'] [codon.translate密码子in poolA]

您将获得:

['TTC','ATG','GCC','CTA','AAC','CAC','GTA','CCG','TAA','AGA']

奖金

biopython库,例如

poolA = ['AAG','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT'] 来自Bio.Seq import Seq 来自Bio.Alphabet import IUPAC [str(Seq(seq,IUPAC.unambiguous_dna)plement())for seq in poolA]

您会得到相同的结果

Bonus 2

修复您的代码,删除不必要的变量 counter

poolA = ['AAG','TAC','CGG','GAT','TTG','GTG','CAT' GGC','ATT','TCT'] complements = {'A':'T','C':'G','T':'A','G':'C'} def match_codons(complementments,poolA): answer = [] for i in poolA: codon =''迭代 for a in i: codon + = complements [a] answer.append(codon)#在次要外部,密码子有三个结束迭代 return answer matching_codons(complementments,poolA)

I'm trying to write a program that converts letters matching a key in a dictionary to the value associated with that key e.g. if the dictionary is {'A':'T', 'C':'G', 'T':'A', 'G':'C'} and the string is 'AAG' the output should be 'TTC'.

EDIT: This is what I've got now thanks to some of your answers:

def matching_codons(complements, poolA): answer = [] codon = '' counter = 0 for i in poolA: for a in i: codon+= complements[a] counter += 1 if counter == 3: answer.append(codon)

Unfortunately this only translates the first 3 letters - how can I make it keep running through the loop?

Note: poolA is a list of strings e.g. ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']

Note2: I can't hard-code anything like a translation table because technically the dictionary input can be changed

解决方案

another solution is using maketrans from string

from string import maketrans complementTrans = maketrans("ACTG", "TGAC") poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT'] [codon.translate(complementTrans) for codon in poolA]

you get:

['TTC', 'ATG', 'GCC', 'CTA', 'AAC', 'CAC', 'GTA', 'CCG', 'TAA', 'AGA']

Bonus

It is always better to use biopython library, for example

poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT'] from Bio.Seq import Seq from Bio.Alphabet import IUPAC [str(Seq(seq, IUPAC.unambiguous_dna)plement()) for seq in poolA]

you get the same result

Bonus 2

Fixing your code, I remove unnecessary variable counter

poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT'] complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'} def matching_codons(complements, poolA): answer = [] for i in poolA: codon = '' # inside of for, codon reset in each iteration for a in i: codon+= complements[a] answer.append(codon) # outside of secondary for, codon have three leters to end iterations return answer matching_codons(complements, poolA)

更多推荐

使用字典转换字母

本文发布于:2023-11-26 03:11:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   字母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!