如何使用count方法从字符串中获取不同的元素

时间:2017-09-22 20:33:22

标签: python string python-3.x count

我正在尝试创建一个提供一些信息的脚本。我需要从向用户询问的列表中计算元素。但是我遇到了一些问题。

例如,给定DNA序列,我需要长度,核苷酸数量及其类型。例如,在如下序列中:AATTAaCCGg输出应为:

Adenine = 4 (count A and a)
Thymine = 2
Cytosine = 2 
Guanine = 2

这是我被困的地方:

sequence = str(input('Enter DNA sequence:'))
print ('Your sequence contain:',len(secuencia), 'bases', 'with the following structure:')
adenine = sequence.count("A" or "a")
thymine = sequence.count("T" or "t")
cytosine = sequence.count("C" or "c")
guanine = sequence.count ("G" or "g")

print("adenine = ", adenine)
print("thymine = ", thymine)
print("cytosine = ", cytosine)
print("guanine = ", guanine)

但是sequence.count部分没有区分“A”和“a”。我试过“和”和“或”结构,但它似乎不起作用。

顺便说一句,这可以用这样的字典来解决:

dicc = {adenine:"A" and "a", thymine:"T" and "t",
       cytosine:"C" and "c", guanine:"G" and "g"}

4 个答案:

答案 0 :(得分:1)

adenine = sequence.count("A") + sequence.count("a")会出现什么问题?

问题是or不会被接受为sequence.count函数的参数。 Python将评估or作为逻辑运算符。像"A""a"这样的字符串是" truthy"值。所以,最后发生的事情是你正在呼叫sequence.count(True),这几乎肯定不会给你你期望的结果。

如果没有创建自己的函数来计算字符串中"A""a"出现的次数,那么您应该处理返回的值。这里它们是整数,所以你可以简单地添加它们。这意味着程序必须搜索字符串两次以找到字符,但这对于这样的程序并不重要。

答案 1 :(得分:0)

单独查找并总结:

...
adenine = sequence.count("A") + sequence.count("a")
thymine = sequence.count("T") + sequence.count("t")
cytosine = sequence.count("C") + sequence.count("c")
guanine = sequence.count ("G") + sequence.count("g")
...

或者将所有字符串设为小写,然后计数:

...
sequence.lower()
adenine = sequence.count("a")
thymine = sequence.count("t")
cytosine = sequence.count("c")
guanine = sequence.count("g")
...

答案 2 :(得分:0)

您可以使用collections.Counter来计算每个字母的出现次数:

import collections

sequence = input('Enter a DNA sequence: ')
counter = collections.Counter(sequence)
print(counter)

例如:

Enter a DNA sequence: AATTAaCCGg
Counter({'A': 3, 'C': 2, 'T': 2, 'G': 1, 'a': 1, 'g': 1})

但是如果你认为“A”类似于“a”,你可以忽略套管:

sequence = input('Enter a DNA sequence: ')
counter = collections.Counter(sequence.upper())
print(counter)

你得到:

Enter a DNA sequence: AATTAaCCGg
Counter({'A': 4, 'C': 2, 'T': 2, 'G': 2})

答案 3 :(得分:-1)

你应该算这样。也许有点矫枉过正,但它很明显是水。

sequence=input('Give me a DNA:')
count_a = 0
count_t = 0
count_c = 0
count_g = 0

for each_letter in sequence.lower():
    if 'a' in each_letter:
        count_a += 1
    elif 't' in each_letter:
        count_t += 1
    elif 'c' in each_letter:
        count_c += 1
    elif 'g' in each_letter:
        count_g += 1

print('The counters are: \n -%s \n -%s \n -%s \n -%s \n', (count_a,count_t,count_c,count_g))