I would like to ask you about the Python sequence list element modification.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

We're working on a simple code that represents the DNA sequence in Python.

Code that stores the base sequence in the list when it is entered, transforms the list element according to the rule with a for statement and an if statement, and outputs the changed list. (Rule: A --> T, T --> A)

But if I did it like I planned it, the for statement was executed, and I changed only a[0] and I didn't touch a[1] and a[2].

So even if I input ATA, the if statement is applied only to a[0] and ['T', 'T', 'A'] is printed out like this. (If it is printed correctly according to the rule, it should be printed as ['T', 'A', 'T']) How do you apply the if statement to a[1], a[2] and output it correctly according to the rule?

DNA_list = list (map(str, input("Enter 3 base sequence").split())) 
a = DNA_list

for i in a:
  if (i == "A") :
    a[a.index("A")] = "T"

  elif (i == "T") :
    a[a.index("T")] = "A" 

  else :
    a = "You entered the wrong sequence"

print(a)

python list

2022-09-20 11:34

1 Answers

a.Index ("A") always tells you the first index. So only the first one will change. There are other problems with the code, but it is.

All you have to do is take the mapping rules that you want to convert and map them one by one. Let's write list compliance.

Mapping rule = {"A":"T", "T":"A"}

seq = [mapping rule [ele] for ele in seq]

You can roughly do it like this.


2022-09-20 11:34

If you have any answers or tips


© 2022 pinfo. All rights reserved.