计算Python字符串的唯一字符总数

编程入门 行业动态 更新时间:2024-10-17 02:59:03
本文介绍了计算Python字符串的唯一字符总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于上述问题,我深陷其中.到目前为止,我想出的代码是:

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)

我的思路是我首先: 1.要求用户输入DNA碱基(ATCG) 2.将用户输入更改为列表 3.回到主要(count_bases)函数,我计算'A','T','C','G'的数量 4.对4个不同的基数使用4条if-else语句.

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.

推荐答案

  • 您的函数get_user_input()没有返回值
  • get_user_input.count('A')包含一个函数调用,如果没有括号,它将无法使用.正确:get_user_input().count('A')
  • 如评论中所述,get_user_input()仅应调用一次,因为用户输入将通过input()函数在每次调用时收集.
  • 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.
  • 这应该有效:

    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

    更多推荐

    计算Python字符串的唯一字符总数

    本文发布于:2023-11-29 12:32:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1646404.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   字符   总数   Python

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!