簡體   English   中英

如何匹配兩個數組的內容並得到相應的索引ruby

[英]how to match the contents of two arrays and get corresponding index ruby

原始問題 :最初的問題是如何迭代到嵌套循環ruby中的下一個元素。 答案教會了解決我的問題的慣用方法,它需要一個不同的問題,以便當用戶在google搜索時,找到正確的東西

我有這個要求,其中有兩個數組,並且每個數組都按排序順序具有唯一值。

array1 = [2,3,4,5,6,7] #can not have repeated values[2,2,3]
array2 = [2,5,7]

我想匹配兩個數組的元素和match found打印match found ,以及兩個數組的索引。 這是可以正常工作的代碼。

array1.each do |arr1|
  array2.each do |arr2|
    if (arr1==arr2)
      puts ("Match found element #{arr1} #{array1.index(arr1)} #{array2.index(arr2)}")
      #some code here to move to the next element in array 1 and array 2 and continue looping from there
    end
  end
end

但是這並沒有使用數據結構的獨特性和以任何方式排序。 例如,在上面的例子中的情況下,元件2array1匹配元件2array2 ,該元件2array2不應繼續嘗試匹配的其它元件array1並且還array1應該被移動到下一個。 我知道next會有一些東西。 但我認為只返回下一個元素並且不會將迭代器移動到下一個元素? 此外,我必須移動到兩個陣列的下一個。 我該怎么做呢?

如果你想在兩個數組之間找到匹配的元素,只需使用& like:

array1 = [2,3,4,5,6,7] #does not have repeated values[2,2,3] and the values are sorted
array2 = [2,5,7]

matches = array1 & array2
 => [2, 5, 7]

要打印在每個數組中找到的匹配和索引,只需循環遍歷matches數組:

matches.each do |number|
  puts "Match found element #{number}"
  puts "array1 index=#{array1.rindex(number)}"
  puts "array2 index=#{array2.rindex(number)}"
end

Match found element 2
array1 index=0
array2 index=0
Match found element 5
array1 index=3
array2 index=1
Match found element 7
array1 index=5
array2 index=2

對於你的代碼,你只需要使用break來退出array2循環,移動到array1中的下一個元素。

array2.each_with_index do |arr1, i1|
   array1.each_with_index do |arr2, i2|
     if (arr1==arr2)
       puts ("Match found element array1: #{arr1} #{i1} and array2: #{arr2} #{i2}")
       #if match element is found, exit from the current loop
       break
     end
   end
    end
Match found element array1: 2 0 and array2: 2 0
Match found element array1: 5 1 and array2: 5 3
Match found element array1: 7 2 and array2: 7 5

要么

(array1 & array2).map{ |e| puts "Match found element #{e}" }
Match found element 2
Match found element 5
Match found element 7

這是另一種方式:

array1 = [2,3,4,5,6,7]
array2 = [2,5,7]

h = array1.each_with_index.to_h
  #=> {2=>0, 3=>1, 4=>2, 5=>3, 6=>4, 7=>5} 

array2.each_with_index.with_object({}) { |(v,i),g| g[v] = [h[v],i] if h.key?(v) }
  #=> {2=>[0, 0], 5=>[3, 1], 7=>[5, 2]} 

第二個表達式可以分解如下:

e0 = array2.each_with_index
  #=> #<Enumerator: [2, 5, 7]:each_with_index> 

我們可以通過將它轉換為數組來查看此枚舉器的元素:

e0.to_a
  #=> [[2, 0], [5, 1], [7, 2]] 

e1 = e0.with_object({})
  #=> #<Enumerator: #<Enumerator: [2, 5,7]:each_with_index>:with_object({})> 
e1.to_a
  #=> [[[2, 0], {}], [[5, 1], {}], [[7, 2], {}]]

枚舉器e的元素被傳遞給塊,並由Enumerator#each分配給塊變量。 第一個(使用Enumerator#next獲得)是:

(v,i),g = e1.next
  #=> [[2, 0], {}] 
v #=> 2 
i #=> 0 
g #=> {} 

我們現在可以執行塊:

g[v] = [h[v],i] if h.key?(v)
  #=> g[2] = [h[2],0] if h.key(v)
  #   g[2] = [0,0] if true

e1的下一個元素現在傳遞給塊:

(v,i),g = e1.next
  #=> [[5, 1], {}] 
v #=> 5 
i #=> 1 
g #=> {2=>[0, 0]} 
g[v] = [h[v],i] if h.key?(v)
  # g[5] = [h[5], 1] if h.key?(5)
  # g[5] = [3, 1] if true

g #=> {2=>[0, 0], 5=>[3, 1]} 

e1的最后一個元素的計算是類似的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM