> For example:
> I want to substitute A with T and G with C and vice
> versa > > A -> T > G -> C > T -> A > c -> G
You can do this with the translate() method of a string. It is a two-step process.
First you have to make a translation table that defines the translation.
> >> import string
> >> xlate = string.maketrans('AGTC', 'TCAG')
Next use the translation table to translate the string of interest:
> >> 'AAGTTC'.translate(xlate) 'TTCAAG'
my example:
import string
a=string.maketrans('abcdefghijklmnopqrstuvwxyz','cdefghijklmnopqrstuvwxyzab')
print "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.".translate(a)
感觉没那么智能 有点像java的replace
本文介绍了一种使用Python的translate方法进行高效字符串替换的方法。通过创建一个转换表,可以轻松实现字符的批量替换,如DNA序列中碱基的互换。这种方法不仅简洁而且执行效率高。

1122

被折叠的 条评论
为什么被折叠?



