Reputation: 39
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.
This part of the code should essentially go through both the original code and the new code, compare them position by position, and determine if that position is equal. If you look at the code below and example would be, position 0 in both has A and A so they are equal. However, position 1 has T and C so it isn't equal. The for loop is meant to cycle through both parts of the list.
The code looks like this:
# Assigning all 4 base letters to a certain list of numbers so it can be edited later based on what the new genetic code has.
A = [1,0,0,0]
C = [0,1,0,0]
T = [0,0,1,0]
G = [0,0,0,1]
# Making two sample pieces of genetic code
original = [A,T,C,G,T,A,G]
copy = [A,C,C,A,T,G,G]
# Going through both the new and original list and comparing values at every position(0-6)
for i in range(original):
if original[i] == copy[i]:
print("They're the same")
else:
print("They're not the same.")
i += 1
I got the error that 'list' object cannot be interpreted as an integer. I've read in stack overflow that I should remove the range part of the for statement. I tried different variations of that and got "list indices must be integers or slices, not list." Upon reading more posts on this error people are saying to add the range. So it's going back and forth.
Does anyone know why both errors come up if I incorporate range or not? Is there another part of the algorithm that I'm not missing. I'm relatively new to python so I might be missing something pretty simple.
Upvotes: 2
Views: 152
Reputation: 1874
To use your existing code:
range
expects an integer
value, you are providing a list
, change it to the length of original instead
also the for loop automatically increments i
- do not increment it yourself at the end of the loop
for i in range(len(original)):
if original[i] == copy[i]:
print("They're the same")
else:
print("They're not the same.")
Upvotes: 1
Reputation: 1937
The other answer in this thread work fine.
As an alternative, you can zip
the two lists and then compare them at the same time. No indexes required.
for o,c in zip(original, copy):
if o == c:
print("They're the same")
else:
print("They're not the same.")
Upvotes: 3
Reputation: 27557
Change your for loop into this:
for i,v in enumerate(original):
if v == copy[i]:
print("They're the same")
else:
print("They're not the same.")
enumerate()
will take in an array, and return a generator that aligns the index and value of the array during each iteration.
Or, you can use zip()
:
for a,b in zip(original,copy):
if a == b:
print("They're the same")
else:
print("They're not the same.")
Upvotes: 4