Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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 DNA序列串中的核苷酸计数_Python - Fatal编程技术网

Python DNA序列串中的核苷酸计数

Python DNA序列串中的核苷酸计数,python,Python,我已经写了这个函数,它的功能不是我想要的。有什么想法吗? 我知道问题出在字符定义上 def count_nucleotides(dna, nucleotide): ''' (str, str) -> int Return the number of occurrences of nucleotide in the DNA sequence dna. >>> count_nucleotides('ATCGGC', 'G') 2 &

我已经写了这个函数,它的功能不是我想要的。有什么想法吗? 我知道问题出在字符定义上

def count_nucleotides(dna, nucleotide):
    ''' (str, str) -> int

    Return the number of occurrences of nucleotide in the DNA sequence dna.

    >>> count_nucleotides('ATCGGC', 'G')
    2
    >>> count_nucleotides('ATCTA', 'G')
    0
    '''

    num_nucleodites=0

    for char in dna:
        if char is ('A'or'T'or'C'or'G'):
            num_nucleodites=num_nucleodites + 1      
    return num_nucleodites

您编写的
条件不能按您认为的方式工作。。它应该检查每个字符的
char
,然后使用
将它们分开,如下所示:-

if char is 'A' or char is 'T' ... so on:
但是,您可以使用一种更好的方法在操作符中使用

试试这个:-

for char in dna:
        if char in 'ATCG':
            num_nucleodites=num_nucleodites + 1 

但是,由于您在
问题中说过,您只需要计算一个特定元素的数量,因此不需要将每个字符与所有4个字符一起检查。。下面是使用basic for循环时代码的外观:-

def count_nucleotides(dna, nucleotide):
    num_nucleotide = 0
    for char in dna:
        if char == nucleotide:
            num_nucleotide = num_nucletode + 1
    return num_nucleotide
或者只是:-

def count_nucleotides(dna, nucleotide):
    return dna.count(nucleotide)

您编写的
条件不能按您认为的方式工作。。它应该检查每个字符的
char
,然后使用
将它们分开,如下所示:-

if char is 'A' or char is 'T' ... so on:
但是,您可以使用一种更好的方法在
操作符中使用

试试这个:-

for char in dna:
        if char in 'ATCG':
            num_nucleodites=num_nucleodites + 1 

但是,由于您在
问题中说过,您只需要计算一个特定元素的数量,因此不需要将每个字符与所有4个字符一起检查。。下面是使用basic for循环时代码的外观:-

def count_nucleotides(dna, nucleotide):
    num_nucleotide = 0
    for char in dna:
        if char == nucleotide:
            num_nucleotide = num_nucletode + 1
    return num_nucleotide
或者只是:-

def count_nucleotides(dna, nucleotide):
    return dna.count(nucleotide)
那就

def count_nucleotides(dna, nucleotide):
    return dna.count(nucleotide)
(请注意,就家庭作业而言,这可能不太可能……)

那就这样吧

def count_nucleotides(dna, nucleotide):
    return dna.count(nucleotide)
(请注意,就家庭作业而言,这可能不太可能实现…)

这就是计算返回“A”的
'A'或'T'
,然后检查
char
是否等于它-在Python REPL中尝试:

>>> ('A'or'T'or'C'or'G')
'A'
我想你的意思是:

if char in ('A', 'T', 'C', 'G'):
这就是计算返回“A”的
'A'或'T'
,然后检查
char
是否等于它-在Python REPL中尝试:

>>> ('A'or'T'or'C'or'G')
'A'
我想你的意思是:

if char in ('A', 'T', 'C', 'G'):

根据你的一条评论,你似乎在寻找不止一个核苷酸的重叠序列。这可以通过正则表达式完成:

import re
def find_overlapping(needle, haystack):
    return len(re.findall("(?=" + needle + ")", haystack))
然后您可以这样使用它:

>>> find_overlapping("AGA", "AGAGAGAAGAGAG")
5

根据你的一条评论,你似乎在寻找不止一个核苷酸的重叠序列。这可以通过正则表达式完成:

import re
def find_overlapping(needle, haystack):
    return len(re.findall("(?=" + needle + ")", haystack))
然后您可以这样使用它:

>>> find_overlapping("AGA", "AGAGAGAAGAGAG")
5


我在这里看到了很多与DNA相关的统计数据。这是一个相当常见的家庭作业问题吗?你是在计算所有核苷酸,还是只计算传入的核苷酸?你的
如果char是('a'或'T'或'C'或'G')
实际上与
如果char是'G'
(由于
操作符的工作原理)是一样的。我在这里看到了很多与DNA相关的统计数据。这是一个相当常见的家庭作业问题吗?你是在计算所有核苷酸,还是只计算传入的核苷酸?你的
如果char是('a'或'T'或'C'或'G')
实际上与
如果char是'G'
(由于
操作符的工作原理)是一样的,但是它不算重叠,看起来你只是在寻找一个核苷酸。如果要查找重叠序列,则需要更复杂的内容。@user1719345此函数与文档字符串中的示例匹配。如果它不正确,你能给出更多的例子吗?我想到了,但它不算数,看起来你只是在寻找一个核苷酸。如果要查找重叠序列,则需要更复杂的内容。@user1719345此函数与文档字符串中的示例匹配。如果不正确,你能给出更多的例子吗?好的,但我想只计算通过的核数据项,但我想只计算通过的核数据项。我想函数只计算通过的核数据项。你通常不应该使用
is
进行相等比较。@TimPietzcker谢谢。编辑:)@user1719345。。是的,这就是我的第二个和第三个代码所做的。第一种方法是在代码中执行操作的唯一方法:)我希望该函数只计算通过的核苷元。通常,您不应该使用
is
进行相等比较。@TimPietzcker,谢谢。编辑:)@user1719345。。是的,这就是我的第二个和第三个代码所做的。第一种方法是执行代码中的操作:)+1,强制查找重叠匹配的
(?=X)
是一种巧妙的技巧。+1,强制查找重叠匹配的
(?=X)
是一种巧妙的技巧。