【问题标题】:count the frequency of each character using the dictionary in python使用python中的字典计算每个字符的频率
【发布时间】:2020-08-19 03:27:20
【问题描述】:

我的程序将字符串作为用户的输入,并使用字典计算每个字符的频率。 输入:

Python programming is fun

预期输出:

{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}

我的代码:

string = input().lower()
dicx = {}
count = 0
for i in string:
    dicx['i'] = ''

print(dicx)

【问题讨论】:

    标签: python-3.x string dictionary


    【解决方案1】:

    使用collections.Counter

    dicx = collections.Counter(string.lower())
    

    【讨论】:

      【解决方案2】:

      您可以迭代字符串并相应地更新字典,并且不需要任何计数变量。

      test_str = input().lower()
      dicx = {} 
        
      for i in test_str: 
          if i in dicx: 
              dicx[i] += 1
          else: 
              dicx[i] = 1
      
      print(dicx)
      

      【讨论】:

        【解决方案3】:

        函数将输入作为字符串并计算字符并将它们存储在字典中

        from typing import Dict
        
        
        char_dict = {} #type: Dict[str, int]
        
        
        def char_count(string: str) -> dict:
            new_string = string.lower()
            for c in new_string:
                if c in char_dict:
                    char_dict[c] += 1
                else:
                    char_dict[c] = 1
            return char_dict
        
        
        if __name__ == "__main__":
            UserString = input("Enter Input String: ")
            CharCount = char_count(UserString)
            print("Characters Count: ", CharCount)
        

        例子:

        Enter Input String: Python programming is fun
        
        Characters Count:  {'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, ' ': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}
        

        【讨论】:

          【解决方案4】:

          方式一:为

              symbols = {}
              for s in inp_str.lower():
                  if s in symbols:
                      symbols[s] += 1
                  else:
                      symbols.update({s: 1})
              print(symbols)
          

          方式2:默认字典

               symbols = defaultdict(int)
               for s in inp_str.lower():
                   symbols[s] += 1
               print(symbols)
          

          方式3:反击

              symbols = Counter(inp_str.lower())
              print(symbols)
          

          【讨论】:

            【解决方案5】:
            def charCounter(string):
                empty = {}
                for i in string.lower():
                    if i in empty.keys():
                        empty[i] += 1
                    else:
                        empty[i] = 1
                return empty
            
            print(charCounter("Oh, it is python"))
            

            【讨论】:

            • 请记住,Stack Overflow 不仅仅是为了解决眼前的问题,而是为了帮助未来的读者找到类似问题的解决方案,这需要了解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。鉴于此,您能否edit 您的答案包括对您正在做什么的解释以及为什么您认为这是最好的方法?
            猜你喜欢
            • 2012-06-04
            • 1970-01-01
            • 1970-01-01
            • 2016-05-18
            • 2021-12-31
            • 1970-01-01
            • 1970-01-01
            • 2011-02-01
            相关资源
            最近更新 更多