简体   繁体   中英

Counting total number of unique characters for Python string

在此处输入图像描述

For my question above, I'm terribly stuck. So far, the code I have come up with is:

def count_bases():
    get_user_input()
    amountA=get_user_input.count('A')
    if amountA == 0:
        print("wrong")
    else:
        print ("right",amountA)

def get_user_input():
    one = input("Please enter DNA bases: ")
    two=list(one)
    print(two)

My line of thinking is that I first:
1. Ask user to enter the DNA bases (ATCG)
2. Change the user input into a list
3. Going back to the main (count_bases) function, I count the number of 'A', 'T', 'C', 'G'
4. Use 4 if-else statements for the four different bases.

So far, my code only works up to the output of the user's input into a list. After that, an error just pops up.
Appreciate it if someone can point the right path out to me!
Thanks.

You can use collections.Counter

from collections import Counter

def get_user_input():
    input_str = input("Please enter DNA bases: ")
    c = dict(Counter('ACCAGGA'))
    return c

def count_bases():
    counts = get_user_input()
    dna_base = list("ATCG")
    for base in dna_base:
        if base not in counts.keys():
            print(f"{base} not found")
        else:
            print(f"{base} count: {counts[base]}")

output when I call count_bases()

Please enter DNA bases: >? ACCAGGCA
A count: 3
T not found
C count: 2
G count: 2

What if you make a temporary set, which by definition only contains unique items/characters? Ie in the lines of unique_chars=set(input_string) ?

And to count everything use a zip with a comprehension? For instance: nr_of_instances=zip(unique_chars, [input_string.count(x) for x in unique_chars])

There are probably easier more elegant ways of doing this, but to use some of the built in functions... Not everyday I use zip, but here it might come in handy?

Some of the mistakes which may invoke errors are as follows:

  • You haven't returned the list of DNA string from get_user_input()
  • Due to some misunderstanding, you have a text get_user_input in the function count_bases() , which is neither a function call or a variable name.

Following code is for your reference, which rectifies the mentioned mistakes:

def count_bases():
    inp = get_user_input()
    amountA = inp.count('A')
    if amountA == 0:
        print("wrong")
    else:
        print ("right",amountA)

def get_user_input():
    one = input("Please enter DNA bases: ")
    two = list(one)
    print(two)
    return two

The above code will provide count of just one base ie 'A'. In order to avail count of all the bases, this may help:

def count_bases():
    inp = list(input("Please enter DNA bases: "))
    for base in 'ATCG':
        count_base = inp.count(base)
        if count_base == 0:
            print("Not found.")
        else:
            print ("Count of ", base," is: ", count_base)

  1. Your function get_user_input() has no return value
  2. get_user_input.count('A') contains a function call, it will not work without parenthesis. Correct: get_user_input().count('A')
  3. As mentioned in the comments, get_user_input() should only be called once since the users input will be collected at each call via the input() function.

This should work:

    def count_bases():
        char_list = get_user_input()
        for char in 'ATCG':
            char_count = char_list.count(char)
            if char_count < 1:
                print(char + " not found")
            else:
                print(char + " count: " + str(char_count))

    def get_user_input():
        one = input("Please enter DNA bases: ")
        two=list(one)
        return two
def count_bases(val: str, character: str) -> int:
    # return the count of character in val
    if character in val:
        return val.count(character)
    else:
        return 0

def get_user_input() -> None:
    # get the input from the user
    value = input('Please enter DNA bases: ')
    # find the unique characters in the input
    characters = ['A', 'T', 'C', 'G']
    # find the count
    for char in characters:
        count = count_bases(value, char)
        if count == 0:
            # do something
        else:
            # do something else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM