Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何通过一个字符串数组循环查找匹配项?_Ruby_Arrays_Loops - Fatal编程技术网

Ruby 如何通过一个字符串数组循环查找匹配项?

Ruby 如何通过一个字符串数组循环查找匹配项?,ruby,arrays,loops,Ruby,Arrays,Loops,我试图循环使用字符串数组遍历标题字符串,并查看数组中的哪些字符串匹配 我的代码运行良好,但我不确定这是否是最有效的方法 重要的是数组中的字符串不必完全匹配标题中的短语。只要标题中的每个单词都在,它们可以是任何顺序。任何帮助都会很好 EX.title = "Apple Iphone 4 Verizon" array = ["iphone apple, verizon iphone", "iphone 3g", "iphone 4", "cool iphone"] 我需要它返回[“iphon

我试图循环使用字符串数组遍历标题字符串,并查看数组中的哪些字符串匹配

我的代码运行良好,但我不确定这是否是最有效的方法

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

EX.title = "Apple Iphone 4 Verizon"
   array = ["iphone apple, verizon iphone", "iphone 3g", "iphone 4", "cool iphone"]
我需要它返回
[“iphone苹果”、“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
results=[]
#循环浏览所有PID,查看是否在标题中找到它们
所有pids=[“iphone 3gs”、“iphone白色4”、“iphone verizon”、“黑色iphone”、“at&t iphone”]
title=“苹果Iphone 4白色Verizon”
所有pid均为pid|
匹配=[]
split_id=pid.downcase.split(“”)
分割id.每个do |名称|
in_title=title.downcase.include?(名称)
如果在标题中==真

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

Array#-
执行设置差异操作<代码>[2]-[1,2,3]=[]
[1,2,3]-[2]=[1,3]

title=“苹果Iphone 4白色Verizon”
所有pids=[“iphone 3gs”、“iphone白色4”、“iphone verizon”、“黑色iphone”、“at&t iphone”]
在title=title.downcase.split中设置\u字符串的\u
所有人都做|
设置\u字符串的\u非\u标题中的\u=pid.downcase.split-设置\u字符串的\u标题中的\u
设置\u字符串的\u不在\u title.empty中?
结束

编辑:将“查找”更改为“全部查找”以返回所有匹配项,而不仅仅是第一个匹配项。

您可以执行以下操作:

>> 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"]
>需要“设置”
=>正确
>>title=“苹果Iphone 4 Verizon”
=>“苹果Iphone 4 Verizon”
>>所有pids=[“iphone苹果”、“verizon iphone”、“iphone 3g”、“iphone 4”、“酷炫iphone”]
=>[“iphone苹果”、“verizon iphone”、“iphone 3g”、“iphone 4”、“酷炫iphone”]
>>title\u set=set.new(title.downcase.split)
=> #
>>all|pids.select{pid | Set.new(pid.downcase.split).subset?title|Set}
=>[“iphone苹果”、“verizon iphone”、“iphone 4”]

您可以对数组差异执行非常类似的操作,但是集合可能会更快,因为它们是作为散列实现的。

您的答案与我的答案几乎相同,而且您的速度比我快。我同意你的回答。我关心的一件事是,随着
all_-pids
array的增长,它会变慢。首先为
all_-pids
中的术语设置一些查找表可能会减少这种影响。出于某种原因,这只返回“iphone white 4”,还应该返回“iphone verizon”@blakecash抱歉,我的错:应该是“find_-all”而不是“find”。我已经编辑了这个示例。我认为,使用Set的这个替代方案更好、更干净,没有使用不必要的辅助变量。很高兴听您这么说!:-)如果这是你想要的答案,你应该接受它(计票旁边的勾号),这样其他人就知道这个问题已经得到了回答。