简体   繁体   English

如何使用Python转换字符串中的每个字符

[英]How to Convert Each Character in a String using Python

I have a sequence of characters that I want to convert them into specified charter. 我有一些字符序列,我想将它们转换为指定的章程。

Seq = "AA" #This the sequence of characters

def complement (Seq):
    for nuc in Seq: # converting the sequence of characters into the desirable character 
        if nuc == 'A':
           comp = 'T'
    return comp

print "The complement of the Sequence AA is", complement(Seq)

When I tried to run the code above, the code is not recognizing the whole sequence of characters and convert them at a time into "T"s; 当我尝试运行上述代码时,代码无法识别整个字符序列,而是一次将其转换为“ T”; however, it is only executing "T" once for the input sequence "AA" 但是,对于输入序列“ AA”,它仅执行一次“ T”

Any idea how to make the code converts each "A" character in the sequence into "T"? 知道如何使代码将序列中的每个“ A”字符转换为“ T”吗?

I appreciate your help! 我感谢您的帮助!

Your script have a few errors. 您的脚本有一些错误。 First, the variable comp doesn't have a initial value and returns only one "T"(last one) because the other get replaced every time they find a "A" in the loop. 首先,变量comp没有初始值,并且仅返回一个“ T”(最后一个),因为每次在循环中找到“ A”时,另一个变量都会被替换。 For what I understand you are trying to substitute some values in the same string. 据我了解,您正在尝试在同一字符串中替换某些值。 I find it easier to build a new string with the characters you want to replace. 我发现使用您要替换的字符来构建新字符串更容易。

Here is my script: 这是我的脚本:

Seq = "AA" #This the sequence of characters

def complement (Seq):
    comp = ''
    for nuc in Seq: # converting the sequence of characters into the desirable character 
         if nuc == 'A':
             comp += 'T'
         else:
             comp += nuc
    return comp

print "The complement of the Sequence AA is", complement(Seq)

First, use string.maketrans to build a translation table. 首先,使用string.maketrans来构建转换表。 That function takes two strings, and builds a table that maps each character of the first string to the corresponding character in the second string. 该函数采用两个字符串,并构建一个表,该表将第一个字符串的每个字符映射到第二个字符串中的对应字符。 Now, you can pass that table to the translate method of the string you want to translate. 现在,您可以将该表传递给要translate的字符串的translate方法。

>>> import string
>>> table = string.maketrans("ATCG", "TAGC")
>>> 'AAAGTC'.translate(table)
'TTTCAG'

It's because of that after the condition you just put the 'T' on comp and at last returned it: 因为在这种情况下,您只需将'T'放到comp ,最后将其返回:

    if nuc == 'A':
       comp = 'T'
return comp

But as a more pythonic way you can use str.replace() : 但是,可以使用str.replace()作为更Python的方式:

>>> Seq = "AA"
>>> Seq.replace('A','T')
'TT'

And if you want to convert each character to a special character based on a condition you can use a list comprehension and join : 如果您想根据条件将每个字符转换为特殊字符,则可以使用列表推导并join

>>> Seq = "AA"
>>> ''.join(['T' for i in Seq if i=='A'])
'TT'

Also based on your task you can have another choices like using regex.Which in that case python comes with re.sub() function to replace a string based on a regex. 同样根据您的任务,您可以有其他选择,例如使用regex。在这种情况下,python附带了re.sub()函数来替换基于regex的字符串。

Ofcourse it is going to be only T . 当然,它只会是T Because your loop is just appending T to comp instead of concatenating. 因为您的循环只是将T附加到comp而不是连接。

Try this: 尝试这个:

Seq = "AA" #This the sequence of characters

def complement (Seq):
    comp = ""    <- declare empty string first
    for nuc in Seq:
        if nuc == 'A':
            comp += 'T'   <-- notice the +
    return comp

print "The complement of the Sequence AA is", complement(Seq)

Here is an basic tutorial for string concatenation and formatting. 是有关字符串连接和格式化的基本教程。

if, you want get complement of sequence of DNA 如果要获得DNA序列的互补

complement = {"A":"T", "C":"G", "G":"C", "T":"A"}
seq = "ACGT"
complement_seq = "".join([complement[b] for b in seq])
complement_seq

this is similar to, 这类似于

complement = {"A":"T", "C":"G", "G":"C", "T":"A"}
seq = "ACGT"
complement_seq = ""
for base in seq:
  complement_seq += complement[base]

complement_seq

you get 你得到

TGCA

better, using biopython library 更好,使用biopython库

from Bio.Seq import Seq
my_seq = Seq("ACGT")
my_seq.complement()

you get 你得到

Seq('TGCA', Alphabet())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中将字符串的每个字符转换为list元素的各个元素? - How to convert each character of a string into individual elements of list element in Python? 如何在 Python 中将字节转换为相同长度的字符串(每个字节 1 个字符)? - How to convert bytes to a string of the same length in Python (1 character for each byte)? 如何在python中将\\ 1字符转换为字符串 - How to convert \1 character as a string in python Python如何将字符转换为字符串 - Python how to convert character to string 如何使用python反转字符串中每个字符的位 - How to reverse the bits of each character in a string using python 如何使用python 3在每个元音后在字符串中添加一个字符 - How to add a character in a string after each vowel using python 3 如何使用Python将字符串中的特定字符序列转换为大写? - How to convert specific character sequences in a string to upper case using Python? 如何将具有多个字符的字符串转换为列表,并且每个字符都是 python 中的字符串 [暂停] - How to convert a string with many characters to a list and that each character is a string in python [on hold] 使用 Python 迭代字符串中的每个字符 - Iterating each character in a string using Python 在python中没有空格的每个字符后将输入字符串转换为列表 - Convert input String to List after each character without spaces in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM