Trying to make function which takes string as input and returns no. of words in whole string

前端 未结 3 1491
借酒劲吻你
借酒劲吻你 2021-01-27 07:13

**It takes Input as a string such as this - \'Nice one\' And Output gives - 4,3 (which is no. Of words in sentence or string) **

function countx(str)
   local cou         


        
3条回答
  •  隐瞒了意图╮
    2021-01-27 07:58

    def countLenWords(s):
       s=s.split(" ")
       s=map(len,s)
       s=map(str,s)
       s=list(s)
       return s
    

    The above functions returns a list containing number of characters in each word

    s=s.split(" ") splits string with delimiter " " (space) s=map(len,s) maps the words into length of the words in int s=map(str,s) maps the values into string s=list(s) converts map object to list

    Short version of above function (all in one line)

    def countLenWords(s):
       return list(map(str,map(len,s.split(" "))))
    

提交回复
热议问题