0
f = ''
da = ['A', 'T', 'G', 'C', ' ']
fnn = []
print(fnn)

con = 0

x = input('Corrupted: ')
nx = list(x)

for nx in nx:
    if nx[con] in da:
        f = f + str(nx[con])
    else:
        pass

fn = f.split()

print(fn)
print(fn[0])

for i in fn:
    if fn[i] not in fnn:
        fnn = fnn.extend(fn[i])
    else:
        pass

print(fnn)

该脚本旨在读取输入,然后删除除 from 之外的所有字母A C G T并删除指向它的任何重复序列,我真的很难让它删除重复序列,我需要做什么?我究竟做错了什么?有没有更快的方法来做到这一点?

4

2 回答 2

1

您的代码做了几件奇怪的事情:

  • nx = list(x)- 为什么把 x 变成一个列表?您可以轻松地遍历字符串
  • for nx in nx(如前所述)
  • if nx[con] in da- 这是为了达到什么目的?什么是骗局?
  • str(nx[con])- nx[0] 已经是一个字符串
  • else: pass- 如果您打算通过,则无需包含 else 条件
  • extend扩展一个列表,所以不需要写my_list = my_list.extend...(事实上你会这样丢失列表)
  • fnn.extend(fn[i])- 如果代码的前一部分工作正常,那么fn[i]可能是一个字符串;而且您可能不想通过字符串扩展列表。

尝试尝试看看这是做什么的:

x = ['cat', 'dog']
x.extend('mouse')
print x

我想你想要的是这样的。请注意使用描述性变量名称来帮助读者理解代码的作用。

permitted_characters = 'ATGC '
corrupted = input('Corrupted: ')

# Remove characters that are not permitted and split string into sequences
sequences = ''.join(c for c in corrupted if c in permitted_characters).split()

# Remove repeated sequences
unique_sequences = []
for sequence in sequences:
    if not sequence in unique_sequences:
        unique_sequences.append(sequence)
于 2013-09-08T01:25:26.867 回答
0

for nx in nx:

好吧,首先,您正在覆盖列表,因此nx[con]无法正常工作。

于 2013-09-08T01:10:56.413 回答