我有兩個陣列,我需要比較它們并僅從第二個陣列中獲取唯一值。
例子:
array_first = [1,2,3,4,5,8,9,10,11] # Array for comparison
array_last = [1,2,6,4,7,5,12] # The array from which you want to get values ??that are not in the other
res: [6,7,12] # The values ??to get
有沒有辦法做到這一點?
uj5u.com熱心網友回復:
只需使用 Array#-
array_first = [1,2,3,4,5,8,9,10,11]
array_last = [1,2,6,4,7,5,12]
array_last - array_first
#=> [6, 7, 12]
來自檔案的參考:
array - other_array→new_array回傳一個新的 Array ,其中只包含
array在 Array 中找不到的那些元素other_array;專案使用eql?[...]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/338552.html
標籤:红宝石
