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

前端 未结 3 1492
借酒劲吻你
借酒劲吻你 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(" "))))
    
    0 讨论(0)
  • 2021-01-27 08:02
    -- Localise for performance.
    local insert = table.insert
    
    local text = 'I am a poor boy straight. I do not need sympathy'
    
    local function word_lengths (text)
        local lengths = {}
        for word in text:gmatch '[%l%u]+' do
            insert (lengths, word:len())
        end
        return lengths
    end
    
    print ('{' .. table.concat (word_lengths (text), ', ') .. '}')
    
    • gmatch returns an iterator over matches of a pattern in a string.
    • [%l%u]+ is a Lua regular expression (see http://lua-users.org/wiki/PatternsTutorial) matching at least one lowercase or uppercase letter:
      • [] is a character class: a set of characters. It matches anything inside brackets, e.g. [ab] will match both a and b,
      • %l is any lowercase Latin letter,
      • %u is any uppercase Latin letter,
      • + means one or more repeats.

    Therefore, text:gmatch '[%l%u]+' will return an iterator that will produce words, consisting of Latin letters, one by one, until text is over. This iterator is used in generic for (see https://www.lua.org/pil/4.3.5.html); and on any iteration word will contain a full match of the regular expression.

    0 讨论(0)
  • 2021-01-27 08:05

    You can find a simple alternative with your new requirements:

    function CountWordLength (String)
      local Results  = { }
      local Continue = true
      local Position = 1
      local SpacePosition
      
      while Continue do
        SpacePosition = string.find(String, " ", Position)
        if SpacePosition then
          Results[#Results + 1] = SpacePosition - Position
          Position = SpacePosition + 1
          -- if needed to print the string
          -- local SubString = String:sub(Position, SpacePosition)
          -- print(SubString)
        else
          Continue = false
        end    
      end
    
      Results[#Results + 1] = #String - Position + 1
      
      return Results  
    end
    
    Results = CountWordLength('I am a boy')
    
    for Index, Value in ipairs(Results) do
      print(Value)
    end
    

    Which gives the following results:

    1
    2
    1
    3
    
    0 讨论(0)
提交回复
热议问题