zoukankan      html  css  js  c++  java
  • Python2018-字符串中字符个数统计

    1 编写程序,完成以下要求:

    • 统计字符串中,各个字符的个数
    • 比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1
    •  1 print("-"*50)
       2 print("*"*50)
       3 currentstr = input("PLease input a sentence, the program will cal the word num:")
       4 
       5 currentstr=currentstr.replace(' ','')## Delete the 'Space' value
       6 newstr=''  #a new string to store the result
       7 for i in currentstr:
       8         if(currentstr.count(i)<=1):# if  there is only one word in currentstr
       9                 newstr=newstr+i
      10                 newstr=newstr+':'
      11                 newstr=newstr+str(1)+' '
      12         else:#the word has 2 or more numbers
      13                 if(newstr.count(i)<1): # has not register in the new string
      14                         newstr= newstr+i
      15                         newstr=newstr+':'
      16                         newstr= newstr+str(currentstr.count(i))+' '
      17 
      18 print("-"*50)
      19 print("The word number in this sentence is : %s"%newstr)
      20 print("-"*50)
      21

    结果如下:

    厚积薄发,开物成务。 德才兼备、知行合一。 自强不息,厚德载物。
  • 相关阅读:
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    我的友情链接
    以太坊:通信协议对象 shh
    以太坊:Truffle 概述
    以太坊:快速入门 Truffle
  • 原文地址:https://www.cnblogs.com/robohou/p/8401705.html
Copyright © 2011-2022 走看看