Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带枚举的返回索引_Python - Fatal编程技术网

Python 带枚举的返回索引

Python 带枚举的返回索引,python,Python,我基本上是想返回DNA字符串的起始索引,其中a和G在接下来的五个字母中占多数 参见代码 def a_g_majority_positions(dna): ### BEGIN SOLUTION list=[] for index, item in enumerate(dna): count=0 x=0 index1=index while count < 5:

我基本上是想返回DNA字符串的起始索引,其中a和G在接下来的五个字母中占多数

参见代码

def a_g_majority_positions(dna):

    ### BEGIN SOLUTION
    list=[]
    for index, item in enumerate(dna):
            count=0
            x=0
            index1=index
            while count < 5:
                letter=dna[index1]
                index1+=1
                count+=1
                if letter == 'A':
                    x+=1
                elif letter == 'G':
                    x+=1
            if x>=3:
                list.append(index)
    return list




    ### END SOLUTION
a_g_majority_positions("AACCGGTTAACCGGTT")
def a_g___大多数位置(dna):
###开始解决方案
列表=[]
对于索引,枚举中的项目(dna):
计数=0
x=0
index1=索引
当计数小于5时:
字母=dna[index1]
index1+=1
计数+=1
如果字母==“A”:
x+=1
elif字母=='G':
x+=1
如果x>=3:
list.append(索引)
返回列表
###最终解决方案
多数席位(“AACCGGTTAACCGGTT”)

我总是得到一个字符串索引超出范围的错误。最后dna的正确答案是[0,1,4,5,8,9]

当剩下的字母少于5个时,您需要打破
for
循环:

def a_g_majority_positions(dna):
    result = list()
    for index, item in enumerate(dna):
        if index + 5 >= len(dna):
            break
        count = 0
        x = 0
        while count < 5:
            letter = dna[index + count]
            count += 1
            if letter == 'A':
                x += 1
            elif letter == 'G':
                x += 1
        if x >= 3:
            result.append(index)
    return result

print(a_g_majority_positions("AACCGGTTAACCGGTT"))
注意


不要将
list
用作变量名。它是内置类,如果将其用作变量名,则会引入难以发现的错误。

当索引大于
len(dna)-5时,需要提前中断函数。否则,您将尝试访问超出范围的
dna[len(dna)]

def a_g_majority_positions(dna):

### BEGIN SOLUTION
list=[]
for index, item in enumerate(dna):
        count=0
        x=0
        index1=index
        if index > len(dna) - 5:
            break
        while count < 5:
            letter=dna[index1]
            index1+=1
            count+=1
            if letter == 'A':
                x+=1
            elif letter == 'G':
                x+=1
        if x>=3:
            list.append(index)
return list




### END SOLUTION
a_g_majority_positions("AACCGGTTAACCGGTT")

# Result [0, 1, 4, 5, 8, 9]
def a_g___大多数位置(dna):
###开始解决方案
列表=[]
对于索引,枚举中的项目(dna):
计数=0
x=0
index1=索引
如果索引>长度(dna)-5:
打破
当计数小于5时:
字母=dna[index1]
index1+=1
计数+=1
如果字母==“A”:
x+=1
elif字母=='G':
x+=1
如果x>=3:
list.append(索引)
返回列表
###最终解决方案
多数席位(“AACCGGTTAACCGGTT”)
#结果[0,1,4,5,8,9]

使用
count
方法对感兴趣的信件进行计数。开始连续跑五次,直到你没有足够的位置:

def a_g_majority_positions(dna):

    lst = []
    for start5 in range(len(dna)-4):
        five = dna[start5:start5+5]
        if five.count('A') + five.count('G') >= 3:
            lst.append(start5)
    return lst
或者,对于单语句版本,检查每个字符是否在“AG”中:

两种情况下的输出都是

[0, 1, 4, 5, 8, 9]

index1
(最初基于
index
)增加五次。当
索引
已经等于
len(dna)-1
时会发生什么?感谢您的指针。。。我补充道:“如果(len(dna)-index)你为什么删除了你的问题而不接受答案?@User973:如果你的问题解决了,不要删除问题的内容,只需单击解决问题的答案旁边的复选标记,表明问题已由该答案解决(并奖励帮助你的人)。
lst = [start5 for start5 in range(len(dna)-4)
       if sum(char in "AG"
              for char in dna[start5:start5+5]) >= 3]
[0, 1, 4, 5, 8, 9]