计算字符的频率直到引入新的字符-python

如何解决计算字符的频率直到引入新的字符-python

我正在编写一个程序来根据用户输入来计算字母的频率,直到“!”为止。被介绍了。以下是我的程序:

list1=[] 

character = "" 
while character != '!' :
      character = input()
      list1.append(character)

result=[]
for alphabet in ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] :
     if list1.count(alphabet) > 0:
            result.append(alphabet)
            result.append(list1.count(alphabet))

 print(result)

但是,显然,我应该已经计算过频率,直到在输入中插入新字符为止。 例如,如果输入为(aabc),则我的程序应将两个“ a”相加,然后移至“ b”。

无论如何,在继续使用新字母之前,我是否需要修改循环以计数频率?

解决方法

d = {}
while True:
    temp = input().strip()
    if temp == '!':
        break
    if temp.islower() is False:
        print("Invalid character")
        continue
    if temp not in d:
        d[temp] = 1
    else:
        d[temp] += 1

我用过字典

d将包含执行后的结果。

,

    import string
    
    alpha = string.printable[10:36]
    # [a-z]
    
    
    # return a dictionary consists of keys of 26 alphabets and values of frequency
    def detect(string):
        di = {}
        for k in alpha:
            di[k] = string.count(k)
        return di
    
    
    text = ""
    
    print('Please input the text,end with a exclamation mark !')
    while text.count('!') == 0:
        text += input()
    
    
    # get rid of any after exclamation mark
    main_text = text.split('!')[0]
    print('result is: ' + str(detect(main_text)))

如果要包含大写字母:


    import string
    
    alpha = string.printable[10:62]
    # [a-Z]
    
    
    # return a dictionary consists of keys of 26 alphabets and values of frequency
    def detect(string):
        di = {}
        for k in alpha:
            di[k] = string.count(k)
        return di
    
    
    text = ""
    
    print('Please input the text,end with a exclamation mark !')
    while text.count('!') == 0:
        text += input()
    
    
    # get rid of any after exclamation mark
    main_text = text.split('!')[0]
    print('result is: ' + str(detect(main_text)))

如果您不想处理string模块,并且只要能识别utf-8字符,就计算每个输入的频率:


    def detect(string):
        di = {}
        for k in string:
            di[k] = string.count(k)
        return di
    
    
    text = ""
    
    print('Please input the text,end with a exclamation mark !')
    while text.count('!') == 0:
        text += input()
    
    
    # get rid of any after exclamation mark
    main_text = text.split('!')[0]
    print('result is: ' + str(detect(main_text)))

最后,如果您只想计算低26个字母:


    import string
    
    alpha = string.printable[10:36]
    # [a-z]
    
    def detect(info):
        di = {}
        for k in info:
            if k in alpha:
                di[k] = info.count(k)
        return di
    
    
    text = ""
    
    print('Please input the text,end with a exclamation mark !')
    while text.count('!') == 0:
        text += input()
    
    
    # get rid of any after exclamation mark
    main_text = text.split('!')[0]
    print('result is: ' + str(detect(main_text)))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?