0

I've been working on an algorithm that involves genetic code. I started by associating all 4 genetic bases, A, C, T, G with a list. A is 1,0,0,0. C is 0,1,0,0. T is 0,0,1,0 and G is 0,0,0,1. There are two different genetic codes, one being the original one and the other being one that was genetically mutated. The algorithm is going to come to conclusions of the data given based on the difference between the two genetic codes. But first, I need to sort of preprocess the data before I can work on the algorithm making conclusions.

What I'm trying to do is, when the code sees a letter in the original code, it should look at the letter in the same position in the copy version. If you look at the code below, an example would be seeing if the first letter in each(A & C) or the second letter in each(T & T) are the same. If they are then the list should not change. For example, in the 2nd position, T & T are the same. Which means the list would stay the same and be: 0,0,1,0. However, if it's not the same, so for example A & C, then the algorithm should overlap them and add both letter. So the code would be 1,0,1,0.

So far, this is what the code is looking like:

A = [1,0,0,0]
C = [0,1,0,0]
T = [0,0,1,0]
G= [0,0,0,1]
original = [A,T,T,G,C,T,A]
copy = [C,T,T,A,T,A,A]
final = original # In case you were wondering the purpose of this line is to make a new variable to hold the end result.
for i,v in enumerate(original):
    if v == copy[i]:
      print(v)
    else:
      print(final.insert(i,copy[i])) 

When I run it I get "list index out of range" and I tried to play with it a little and delete the final = original and for some reason it works but instead of combining the two different letters when it should, it just says None.

I'm pretty new to programming so this could be a simple issue but I was wondering how I can actually go about making the two letters from two different lists, overlap if they are different.

Rishi
  • 41
  • 4

1 Answers1

1

Lists are "mutable" in python , in your code by final = original your final name is a new 'reference' to the the list named 'original', but not a new list and any changes made to the underlying list using either name will affect both (or rather will be visible using both list names, but change is only in one place). Use of mutable objects is usually the source of coders pains. You can use final = original.copy() to make a copy and operate on it safely. See other discussions on SO of Are Python Lists mutable?. Easy to trip over it when you are starting.

predmod
  • 399
  • 4
  • 6