【问题标题】:Counting the frequency of a character until a new character is introduced - python计算一个字符的频率,直到引入一个新字符 - python
【发布时间】:2021-01-15 17:24:33
【问题描述】:

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

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'。

在继续使用新字母表之前,我是否可以修改循环以计算频率?

【问题讨论】:

  • 您需要的搜索词组是“run-length encoding”。

标签: python list while-loop word-frequency


【解决方案1】:
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 将包含执行后的结果。

【讨论】:

    【解决方案2】:
    
        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)))
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多