在if语句中设置变量

时间:2022-08-26 22:38:20

I have started Roaslind, the bioinformatics-teaching platform. I am trying to do the first problem. This is the code that I have ended up with:

我已经开始了生物信息学教学平台Roaslind。我想做第一个问题。这是我最终得到的代码:

#!/usr/bin/python -tt

def count_nuc():

    '''Usage: count_nuc()'''

    s = raw_input('Enter nucleotide string: ')
    if s.isalpha():        
        if len(s) < 1000:
            for letter in s:
                if letter.upper() == 'A':
                    a =  s.count('A')
                elif letter.upper() == 'C':
                    c =  s.count('C')
                elif letter.upper() == 'G':
                    g = s.count('G')
                elif letter.upper() == 'T':
                    t = s.count('T')
                else:
                    print('Error')
            print '%d %d %d %d' % (a, c, g, t)
        else:
            print('String must be 1000 nucleotides or less.')
            count_nuc()
    else:
        print('String must of nucleotides must only contain alphabetic characters.')
        count_nuc()

It works fine. Mostly. the issue that I am having is kind of silly. I am using the count method for strings to count the number of nucleotides of a specific type, then assigning that number to my variable. However, if no such nucleotide exists (i.e., I give it a string like 'ATGTTT', then my variable 'c' never gets defined and the print statement barfs. I have thought about this and nothing I can think of allows me to get around this. I have thought of trying to check if the variable exists before printing it out, but this seems kind of clumsy to me and am not sure if that would be considered proper coding etiquette.

它工作正常。大多。我遇到的问题有点愚蠢。我使用字符串的count方法来计算特定类型的核苷酸数,然后将该数字分配给我的变量。但是,如果不存在这样的核苷酸(即,我给它一个像'ATGTTT'那样的字符串,那么我的变量'c'永远不会被定义,并且print语句barfs。我已经考虑过这个,我能想到的任何东西都不能让我得到我已经考虑过在打印之前检查变量是否存在,但这对我来说似乎有些笨拙,我不确定这是否算是正确的编码礼仪。

3 个解决方案

#1


1  

One solution might be assigning all variables to zero before the loop.

一种解决方案可能是在循环之前将所有变量分配为零。

For example:

例如:

a = c = g = t = 0

a = c = g = t = 0

#2


3  

Your counting algorithm is doing a lot more work than necessary. You count all the "A"s in your sequence every time you see an "A" while you are iterating. Instead, you could skip the iteration and just do each of the counts once unconditionally (if you count something that doesn't appear in the string, you'll get a 0):

你的计数算法正在做更多的工作而不是必要的。每次在迭代时看到“A”时,都会计算序列中的所有“A”。相反,您可以跳过迭代并且无条件地执行每个计数(如果计算字符串中没有出现的内容,则会得到0):

if len(s) < 1000:
    a = s.count('A')
    c = s.count('C')
    g = s.count('G')
    t = s.count('T')
    print '%d %d %d %d' % (a, c, g, t)

If you want to do the counting manually, you need to change your assignments. Initialize the variables to 0, then add one to the appropriate one when you see the relevant letter:

如果要手动进行计数,则需要更改分配。将变量初始化为0,然后在看到相关字母时将其添加到相应的变量:

if len(s) < 1000:
    a = c = g = t = 0
    for letter in s:
        if letter.upper() == 'A':
            a += 1
        elif letter.upper() == 'C':
            c += 1
        elif letter.upper() == 'G':
            g += 1
        elif letter.upper() == 'T':
            t += 1
        else:
            print('Error')
    print '%d %d %d %d' % (a, c, g, t)

Or, better yet, let Python's standard library do the counting for you:

或者,更好的是,让Python的标准库为您计算:

from collections import Counter

if len(s) < 1000:
    counts = Counter(s)
    print "{A} {C} {G} {T}".format(counts)

#3


0  

You can also do

你也可以这样做

a = s.count('A') if letter.upper() == 'A' else 0

to move the logic "inline".

移动逻辑“内联”。

#1


1  

One solution might be assigning all variables to zero before the loop.

一种解决方案可能是在循环之前将所有变量分配为零。

For example:

例如:

a = c = g = t = 0

a = c = g = t = 0

#2


3  

Your counting algorithm is doing a lot more work than necessary. You count all the "A"s in your sequence every time you see an "A" while you are iterating. Instead, you could skip the iteration and just do each of the counts once unconditionally (if you count something that doesn't appear in the string, you'll get a 0):

你的计数算法正在做更多的工作而不是必要的。每次在迭代时看到“A”时,都会计算序列中的所有“A”。相反,您可以跳过迭代并且无条件地执行每个计数(如果计算字符串中没有出现的内容,则会得到0):

if len(s) < 1000:
    a = s.count('A')
    c = s.count('C')
    g = s.count('G')
    t = s.count('T')
    print '%d %d %d %d' % (a, c, g, t)

If you want to do the counting manually, you need to change your assignments. Initialize the variables to 0, then add one to the appropriate one when you see the relevant letter:

如果要手动进行计数,则需要更改分配。将变量初始化为0,然后在看到相关字母时将其添加到相应的变量:

if len(s) < 1000:
    a = c = g = t = 0
    for letter in s:
        if letter.upper() == 'A':
            a += 1
        elif letter.upper() == 'C':
            c += 1
        elif letter.upper() == 'G':
            g += 1
        elif letter.upper() == 'T':
            t += 1
        else:
            print('Error')
    print '%d %d %d %d' % (a, c, g, t)

Or, better yet, let Python's standard library do the counting for you:

或者,更好的是,让Python的标准库为您计算:

from collections import Counter

if len(s) < 1000:
    counts = Counter(s)
    print "{A} {C} {G} {T}".format(counts)

#3


0  

You can also do

你也可以这样做

a = s.count('A') if letter.upper() == 'A' else 0

to move the logic "inline".

移动逻辑“内联”。