我一直在研究一种涉及遗传密码的算法。我首先把所有的4个遗传基础,A,C,T,G与一个列表联系起来。A为1 00.0。C为0,1,0。T是0,0,1,0,G是0,0,0,1。有两个不同的遗传密码,一个是原始的,另一个是遗传变异的。该算法将根据两种遗传密码的不同,得出所给数据的结论。但首先,我需要对数据进行预处理,然后才能对算法做出结论。
代码的这一部分基本上应该同时遍历原始代码和新代码,按位置对它们进行比较,并确定该位置是否相等。如果您查看下面的代码并举例说明,在A和A中的位置0都是相等的。但是,位置1有T和C,所以它不相等。for循环用于循环列表的两个部分。
代码如下所示:
# 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 我得到的错误是,“list”对象不能解释为整数。我在堆栈溢出中看到应该删除for语句的范围部分。我尝试了不同的变体,得到了“列表索引必须是整数或切片,而不是列表”。在阅读更多关于此错误的帖子时,人们会说要添加范围。所以它是来回的。
有没有人知道,如果我合并了范围,为什么会出现两个错误?算法的另一部分我没有遗漏。我对python还比较陌生,所以我可能忽略了一些非常简单的东西。
发布于 2020-06-15 14:07:47
将for循环更改为:
for i,v in enumerate(original):
if v == copy[i]:
print("They're the same")
else:
print("They're not the same.")enumerate()将接收一个数组,并返回一个生成器,该生成器在每次迭代时对齐数组的索引和值。
或者,您可以使用zip()
for a,b in zip(original,copy):
if a == b:
print("They're the same")
else:
print("They're not the same.")发布于 2020-06-15 14:14:09
这个线程中的另一个答案很好。作为另一种选择,您可以对这两个列表进行zip,然后将它们同时进行比较。不需要索引。
for o,c in zip(original, copy):
if o == c:
print("They're the same")
else:
print("They're not the same.")发布于 2020-06-15 14:08:35
要使用您现有的代码:range需要一个integer值,您正在提供一个list,将其更改为原始值的长度,同时for循环也会自动递增i --不要在循环结束时自己增加
for i in range(len(original)):
if original[i] == copy[i]:
print("They're the same")
else:
print("They're not the same.")https://stackoverflow.com/questions/62389909
复制相似问题