Counting Total Number Of Unique Characters For Python String
Solution 1:
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
Solution 2:
What if you make a temporary set, which by definition only contains unique items/characters? I.e. 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?
Solution 3:
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 functioncount_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 i.e. '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)
Solution 4:
- Your function get_user_input() has no return value
- get_user_input.count('A') contains a function call, it will not work without parenthesis. Correct:
get_user_input().count('A')
- 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
Solution 5:
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
Post a Comment for "Counting Total Number Of Unique Characters For Python String"