如何遍历数组中的多个字符串

Posted

技术标签:

【中文标题】如何遍历数组中的多个字符串【英文标题】:How to Iterate through multiple strings in an array 【发布时间】:2019-04-29 19:33:17 【问题描述】:

我有一个字符串数组。

a = "Apple Banana oranges grapes. free free free phones. deals deals time.black white grey"
b = a.split(/\./)

我想检查每个字符串中是否存在单词"free""deals",并对其进行计数并将其存储到新数组中。

c = 0
d = 0
g = Array.new
t = Array.new
b.each do |i|
  if /free/.match(i)
    t.push i
    c = c + 1
  elsif /deals/.match(i)
    t.push i
    d = d + 1
  else
    g.push i
  end
end
p d
p c
p g
p t

但它没有显示确切的计数。有没有其他方法可以解析数组中的字符串?

【问题讨论】:

你能澄清一下你的预期输出是什么吗?您想在第一个字符串中显示deals 的计数和free 的计数吗? p a.split.count "deals" 会起作用吗? 【参考方案1】:

通过一些变量重命名,以及 scan 的一些技巧而不是匹配,这可能有效吗?

a = "Apple Banana oranges grapes. free free free phones. deals deals time.black white grey"
sentences = a.split(/\./) # Split on `.`

# Initialize variables
free_count = 0
deal_count = 0
no_match = []
matches = []

sentences.each do |i| # Go through each sentence
  if (m = i.scan(/free/)) && m.any? # Try matching "free", and check if there is any matches
    matches << i # Add match to matches array
    free_count += m.length # Count up amounts of `free` spotted.
  elsif (m = i.scan(/deals/)) && m.any? # Try matching "deals", and check if there is any matches
    matches << i # Add match to matches array
    deal_count += m.length # Count up amounts of `deals` spotted.
  else
    no_match << i # Count up for nothing spotted
 end
end

p free_count #=> 3
p deal_count #=> 2
p no_match #=> ["Apple Banana oranges grapes", "black white grey"]
p matches #=> [" free free free phones", " deals deals time"]

【讨论】:

谢谢您,另外请注意-您能告诉我一种在多个字符串数组中迭代字符串的方法吗?【参考方案2】:

您的方法计算句子,而不是匹配句子中单词的出现次数。使用c += i.split.count "free" 计算单词"free" 的实际出现次数。除非含义明确,否则避免使用单字母变量名。

然而,这一切似乎有点额外的提升;您可以使用builtin array methods 执行计数并选择/拒绝与模式匹配的项目:

a = "Apple Banana oranges grapes. free free free phones. deals deals time.black white grey"

p a.split(".").grep /\bfree\b|\bdeals\b/
p a.split(".").reject |e| e =~ /\bfree\b|\bdeals\b/
p a.split.count "free"
p a.split.count "deals"

输出:

[" free free free phones", " deals deals time"]
["Apple Banana oranges grapes", "black white grey"]
3
2

Try it!

【讨论】:

谢谢您,另外请注意-您能告诉我一种在多个字符串数组中迭代字符串的方法吗? 在 Ruby 中,可能有很多方法可以做到这一点。您可以在数组上使用.each,在字符串上使用.each_char 来迭代字符。【参考方案3】:

可能是遵循您的原始代码的解决方案:

a = "Apple Banana oranges grapes. free free free phones. deals deals time.black white grey"
b = a.split(/\W+/)

c = 0
d = 0
g = Array.new
t = Array.new
b.each do |i|
  if i == "free"
    t.push i
    c = c + 1
  elsif i == "deals"
    t.push i
    d = d + 1
  else
    g.push i
  end
end

puts "Original words: #b"
puts "Counts of 'free': #c"
puts "Counts of 'deals': #d"
puts "Array of matches: #t"
puts "Array of non matches: #g"

输出是:

# Original words: ["Apple", "Banana", "oranges", "grapes", "free", "free", "free", "phones", "deals", "deals", "time", "black", "white", "grey"]
# Counts of 'free': 3
# Counts of 'deals': 2
# Array of matches: ["free", "free", "free", "deals", "deals"]
# Array of non matches: ["Apple", "Banana", "oranges", "grapes", "phones", "time", "black", "white", "grey"]


使用 Ruby 工具进行计数的示例:
counts = 
a.split(/\W+/).tap  |b| b.uniq.map  |w| counts[w.downcase] = b.count(w)  
counts #=> "apple"=>1, "banana"=>1, "oranges"=>1, "grapes"=>1, "free"=>3, "phones"=>1, "deals"=>2, "time"=>1, "black"=>1, "white"=>1, "grey"=>1

然后就可以访问数据了,例如:

counts.keys #=> ["apple", "banana", "oranges", "grapes", "free", "phones", "deals", "time", "black", "white", "grey"]
counts['free'] #=> 3
counts['deals'] #=> 2
counts.select  |_, v| v > 1 #=> "free"=>3, "deals"=>2

【讨论】:

以上是关于如何遍历数组中的多个字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 中遍历数组并获取键值

如何遍历数组中的数组?

handlebars 遍历数组判断是不是含有字符

数组中的元素属性有值,但遍历出来的时候该属性值却为null?

我如何遍历表单数组并将其值设置为打字稿中的另一个数组?

求jsp json数组遍历方法