0

How do I replace the letter in given list ?

A should be replaced with T in given string -> ['GACGTAGCTAGCGTA' , 'TTCCCTAGCGTA' ]

A -> T
G -> C
T -> A
C -> G

I am reading letter by letter and printing it after that got stuck how to achieve below output.

for j in str(lst):
    
    '''

    A -> T
    G -> C
    T -> A
    C -> G
    N
    
    '''    
    
    if j == 'A':
        
        j='T'
      
        
    elif j == 'G' :
        
        j='C'
    
    elif j == 'T' :
        
        j='A'
        
    elif j == 'C' :
        
        j='G'       
        
    else:
        
        pass

    
print(j)

Output:

[ 'CTGCATCGATCGCAT' , 'AAGGGATCGCAT' ]

4 Answers4

4

You can use str.maketrans to create a translation table and then str.translate to get the desired result:

>>> lst = ['GACGTAGCTAGCGTA' , 'TTCCCTAGCGTA']

>>> for item in lst:
...     print(item.translate(item.maketrans('ATGC', 'TACG')))
...
CTGCATCGATCGCAT
AAGGGATCGCAT

For the sake of completeness, here is the result in a list:

lst = ['GACGTAGCTAGCGTA' , 'TTCCCTAGCGTA']
my_list = [item.translate(item.maketrans('ATGC', 'TACG')) for item in lst]
print(my_list)
Asocia
  • 5,935
  • 2
  • 21
  • 46
  • Partially worked it does not print data in list format `my_list = [CTGCATCGATCGCAT,AAGGGATCGCAT]` –  Sep 11 '21 at 17:16
  • 1
    well, you can create a list instead of printing... Just do: `my_list.append(...)` inside the loop. If you don't know how to do that you need to learn the basics first. – Asocia Sep 11 '21 at 17:17
  • 1
    To expand on the above comment, a big part of programming is separation of concerns. If your problem is "I need to do something to each element of the list", you need to be able to separate the "do something" from the "each element of the list" part. This answer provides the first part, and the above comment provides the second. If you can't put those together, then you need to learn how to work with lists before copying code that works with lists off the Internet and hoping it magics your problem away. – Silvio Mayolo Sep 11 '21 at 18:27
4

I am not sure I understand your question, but the translate() function can be used to map characters in a string using a dictionary :

table = {'G':'C', 'A':'T', 'C':'G', 'T':'A'}
string = 'GACGTAGCTAGCGTA'
print(string.translate(table))

The output is : 'GACGTAGCTAGCGTA'

Hope it answers your question.

hanoub__
  • 41
  • 2
1

You're modifying a local variable j, not changing any aspect of the string itself. You probably want to make a new string with the result.

example_input = "AACGT"
output = ""
for x in example_input:
    if j == 'A':
        j = 'T'
    elif j == 'G':
        j = 'C'
    elif j == 'T':
        j = 'A'
    elif j == 'C':
        j = 'G'       
    output += j
print(output)
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 2
    More context: https://stackoverflow.com/questions/14814771/do-python-for-loops-work-by-reference – jeanluc Sep 11 '21 at 17:06
  • @Silvio Mayolo : How to read this list values as input -> `['GACGTAGCTAGCGTA' , 'TTCCCTAGCGTA' ]` and display output : `[ 'CTGCATCGATCGCAT' , 'AAGGGATCGCAT' ]` .. your code works with only one string ... need to pass input values from the list –  Sep 11 '21 at 17:09
1

You could use a dictionary for this. The original letters will be the key and the updated one will be the corresponding value. Then you can just iterate through the string and get the appropriate letter.

my_list = ['GACGTAGCTAGCGTA' , 'TTCCCTAGCGTA' ] 
ans = []
for entry in my_list:
    s = ""
    wordMap = { 'A' : 'T', 'G':'C', 'T':'A', 'C' :'G'}
    for i in entry:
        s+=wordMap[i]
    ans.append(s)

print(ans)
    

Output

['CTGCATCGATCGCAT', 'AAGGGATCGCAT']
vnk
  • 1,060
  • 1
  • 6
  • 18