统计一个字符串中每个字符的出现次数

    • 封装错误

      • 长度错误封装```python class LenError(Exception): def init(self, Array_len, Get_Number): self._Array_len = Array_len self._Get_Number = Get_Number

        def str(self): return f”The maximum length of the object is {self._Array_len}, “ \

        1. f"and the {self._Get_Number} you enter exceeds the maximum limit"

        ```

      • 数值范围错误封装```python class IntError(Exception): def init(self, _Get_Number): self._Get_Number = _Get_Number

        def str(self): return f”The value entered must be greater than or equal to 0, “ \

        1. f"but the value participating in the operation is {self._Get_Number}"

        ```

      • 字符范围错误封装```python class NotInError(Exception): def init(self, _Get_Char, Get_Str): self._Get_Char = _Get_Char self._Get_Str = Get_Str

        def str(self): return f”The ‘{self._Get_Char}’ character you are looking “ \

        1. f"for has not appeared in this string '{self._Get_Str}'"

        ```

    • 功能封装```python class character: def init(self):

      1. self._Get_Str = None
      2. self._top_Count = 0
      3. self._InChar = None

      @property def Get_Str(self):

      1. return self._Get_Str

      @Get_Str.setter def Get_Str(self, Get_Str):

      1. self._Get_Str = Get_Str

      @property def top_Count(self):

      1. return self._top_Count

      @top_Count.setter def top_Count(self, top_Count):

      1. self._top_Count = top_Count

      @property def Clear_Space(self):

      1. self._Get_Str = self._Get_Str.replace(" ", "")
      2. return self._Get_Str

      @property def InChar(self):

      1. return self._InChar

      @InChar.setter def InChar(self, InChar):

      1. self._InChar = InChar

      @property def Get_word_count(self):

      1. return self._Get_word_count()

      @property def Get_top_count(self):

      1. self._IntError()
      2. return self._Get_top_count(top_Count=self.top_Count)

      @property def IsInStr(self):

      1. _return = False
      2. if self._InChar in self._Get_Str:
      3. _return = True
      4. else:
      5. raise NotInError(self._InChar, self._Get_Str)
      6. return _return

      @property def Number_of_appearances(self):

      1. if self.IsInStr is True:
      2. return self._Number_of_appearances()
      3. else:
      4. raise NotInError(self._InChar, self._Get_Str)

      def _IntError(self):

        if self.top_Count <= 0:
            raise IntError(self.top_Count)
      

      def __str_to_list(self):

        return list(str(self._Get_Str))
      

      def _Get_word_count(self):

        return Counter(self.__str_to_list())
      

      def _Get_top_count(self, top_Count):

        if self._len_word_count() >= self.top_Count:
            return self._Get_word_count().most_common(top_Count)
        else:
            raise LenError(self._len_word_count(), self.top_Count)
      

      def _len_word_count(self):

        return self._Get_word_count().__len__()
      

      def _Number_of_appearances(self):

        return dict(self.Get_word_count)[self.InChar]
      

      ```

      • 详解说明| 函数功能 | 功能 | | —- | —- | | Get_Str(self) | 字符串参数输出 | | Get_Str(self, Get_Str) | 字符串参数输入 | | top_Count(self) | 得到前几个元素参数数目 | | top_Count(self, top_Count) | 输入前几个元素参数数目 | | Clear_Space(self) | 清空字符串的所有空格 | | InChar(self) | 字符串匹配字符参数输出 | | InChar(self, InChar) | 字符串匹配字符参数输人 | | Get_word_count(self) | 得到所有字符出现次数元素信息 | | Get_top_count(self) | 得到出现次数排在前n的元素信息 | | IsInStr(self) | 得到要查找的元素是否出现在字符串中 | | Number_of_appearances(self) | 得到要查找元素在其中出现了几次 |
    • 举例python if __name__ == '__main__': one = character() one.Get_Str = "hello world" clear_space = one.Clear_Space print(clear_space) print(one.Get_Str) print(one.Get_word_count) one.top_Count = 2 ones = one.Get_top_count print(ones) print(ones[0]) one.InChar = 'o' print(one.InChar) print(one.IsInStr) print(one.Number_of_appearances) """ :returns hello world helloworld helloworld Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1}) [('l', 3), ('o', 2)] ('l', 3) o True 2 """