簡體   English   中英

計算 Python 字符串的唯一字符總數

[英]Counting total number of unique characters for Python string

在此處輸入圖像描述

對於我上面的問題,我非常卡住。 到目前為止,我想出的代碼是:

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)

我的思路是我首先:
1. 要求用戶輸入 DNA 鹼基 (ATCG)
2.將用戶輸入改為列表
3.回到主(count_bases)function,我數了'A','T','C','G'的個數
4. 對四個不同的基數使用 4 個 if-else 語句。

到目前為止,我的代碼只適用於用戶輸入到列表中的 output。 之后,就會彈出一個錯誤。
如果有人可以為我指出正確的道路,將不勝感激!
謝謝。

您可以使用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當我調用count_bases()

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

如果您制作一個臨時集,根據定義,它只包含唯一的項目/字符怎么辦? 即在unique_chars=set(input_string)的行中?

並計算一切使用 zip 與理解? 例如: nr_of_instances=zip(unique_chars, [input_string.count(x) for x in unique_chars])

這樣做可能有更簡單更優雅的方法,但是要使用一些內置函數......我不是每天都使用 zip,但在這里它可能會派上用場?

一些可能引發錯誤的錯誤如下:

  • 您尚未從get_user_input()返回 DNA 字符串列表
  • 由於一些誤解,您在 function count_bases()中有一個文本get_user_input ,它既不是 function 調用也不是變量名稱。

以下代碼供您參考,它糾正了提到的錯誤:

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

上面的代碼將只提供一個基數,即“A”。 為了利用所有基地的數量,這可能會有所幫助:

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. 您的 function get_user_input() 沒有返回值
  2. get_user_input.count('A') 包含一個 function 調用,沒有括號它將無法工作。 正確: get_user_input().count('A')
  3. 正如評論中提到的,get_user_input() 應該只調用一次,因為用戶輸入將在每次調用時通過input() function 收集。

這應該有效:

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM