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

僤鯓⒐⒋嵵緔 提交于 2021-02-05 09:31:06

问题


**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 count = {}
   for i = 1, string.len(str) do
       s = ''
       while (i<=string.len(str) and string.sub(str, i, i) ~= ' ' ) do
           s = s .. string.sub(str, i, i)
           i = i+1
       end
       if (string.len(s)>0) then
           table.insert(count,string.len(s))
       end
   end
   return table.concat(count, ',')
end

回答1:


-- 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.




回答2:


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



回答3:


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(" "))))


来源:https://stackoverflow.com/questions/64293996/trying-to-make-function-which-takes-string-as-input-and-returns-no-of-words-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!