如何使用字符串数组遍历字符串以查找匹配项?

时间:2011-05-28 03:30:35

标签: ruby arrays loops

我正在尝试使用字符串数组遍历标题字符串,并查看数组中哪些匹配。

我的代码工作正常,但我不确定这是否是最有效的方法。

重要的是数组中的字符串不必与标题中的短语完全匹配。只要每个单词都在标题中,它们就可以按任何顺序排列。任何帮助都会很棒。

EX.title = "Apple Iphone 4 Verizon"
   array = ["iphone apple, verizon iphone", "iphone 3g", "iphone 4", "cool iphone"]

我需要它才能返回["iphone apple", "verizon iphone", "iphone 4"]。字符串“verizon iphone”和“iphone apple”中的单词在标题中,顺序无关紧要

results = [] 

#Loop through all the pids to see if they are found in the title
all_pids = ["iphone 3gs", "iphone white 4", "iphone verizon", "black iphone", "at&t      iphone"]
title = "Apple Iphone 4 White Verizon"
all_pids.each do |pid|
    match = []
    split_id = pid.downcase.split(' ')
    split_id.each do |name|

      in_title = title.downcase.include?(name) 
      if in_title == true
        match << name
      end
    end

    final = match.join(" ")

    if final.strip == pid.strip
      results << pid
    end

end

print results

当我运行它时,它打印出我需要的["iphone white 4", "iphone verizon"]

2 个答案:

答案 0 :(得分:2)

在我看来,您希望找到由与标题中的字符串严格相交的字符串组成的字符串。

Array#-执行设置差异操作。 [2] - [1,2,3] = [][1,2,3] - [2] = [1,3]

title = "Apple Iphone 4 White Verizon"
all_pids = ["iphone 3gs", "iphone white 4", "iphone verizon", "black iphone", "at&t      iphone"]
set_of_strings_in_title = title.downcase.split
all_pids.find_all do |pid|
  set_of_strings_not_in_title = pid.downcase.split - set_of_strings_in_title 
  set_of_strings_not_in_title.empty?
end

编辑:将#find更改为#find_all以返回所有匹配,而不仅仅是第一次。

答案 1 :(得分:2)

您可以执行以下操作:

>> require 'set'
=> true
>> title = "Apple Iphone 4 Verizon"
=> "Apple Iphone 4 Verizon"
>> all_pids = ["iphone apple", "verizon iphone", "iphone 3g", "iphone 4", "cool iphone"]
=> ["iphone apple", "verizon iphone", "iphone 3g", "iphone 4", "cool iphone"]
>> title_set = Set.new(title.downcase.split)
=> #<Set: {"apple", "iphone", "4", "verizon"}>
>> all_pids.select { |pid| Set.new(pid.downcase.split).subset? title_set }
=> ["iphone apple", "verizon iphone", "iphone 4"]

你可以做一些与数组差异非常相似的东西,但是因为它们被实现为哈希值,因此设置可能会更快。