1

我想我在Ruby中让自己非常困惑......

我有一个数组:

array1 = [["4b411f2bf964a52082c125e3", "The Three Pigeons", 51.236318, -0.57055], ["4b444648f964a52049f325e3", "The Royal Oak", 51.23555937678702, -0.5702378403809515], ["4b92c695f964a520aa1a34e3", "Slug And Lettuce", 51.237156, -0.571021], ["4b490136f964a520a56126e3", "The Robin Hood", 51.23603403568268, -0.568686], ["4b425f85f964a52092d225e3", "The Guildford Tup", 51.237734, -0.5703823], ["4b48f87ff964a520096026e3", "The Keep", 51.234704, -0.572574], ["4b426369f964a520e3d225e3", "The Five & Lime", 51.236908, -0.573695], ["4b56243af964a5204f0228e3", "The Albany", 51.23687122552597, -0.5666781994529876], ["4b426047f964a520a4d225e3", "The Kings Head", 51.234176, -0.573656], ["4b4261e4f964a520c6d225e3", "The Live and Let Live", 51.238477, -0.573306], ["4b425ec9f964a52086d225e3", "The Star Inn - Shepherd Neame", 51.23501026190194, -0.5749610066413879], ["4cb995490180721e03e09461", "Prince Albert", 51.242471, -0.572899], ["4e02726dc65b8061424b59f1", "Bar Mambo", 51.236896, -0.577263], ["4b7451e5f964a520fad42de3", "The Rodboro Buildings (Wetherspoon)", 51.236624141592365, -0.5775332450866699], ["4b6de739f964a520769a2ce3", "The White House", 51.23463575311113, -0.5773776769638062], ["4bb504a30ef1c9b6dbc2f412", "The Britannia - Shepherd Neame", 51.233105063438416, -0.5760687589645386], ["4b4447f8f964a5206cf325e3", "The George Abbot", 51.235186599066246, -0.5779409408569336], ["4b894378f964a520e72632e3", "The Boatman", 51.23155028087051, -0.572927], ["4bb475f449bdc9b65bcb0c10", "The Keystone", 51.23437208365849, -0.5779758095741272], ["4ba55291f964a5209afa38e3", "Rogues Bar", 51.23763173808256, -0.5610001087188721], ["4b40ce65f964a5205ebb25e3", "The Drummond", 51.24133950313797, -0.5758380889892578], ["4b48c8a8f964a520cb5626e3", "The Stok", 51.24272843225208, -0.5718989403261525], ["4c4f275651c2c9288af1859f", "The Parkway", 51.248229, -0.569356], ["4b48a60ff964a5208c5126e3", "The King's Head", 51.24666037427897, -0.5728936419289142], ["4b9a86c4f964a520f2bd35e3", "Ye Olde Ship Inn", 51.225673503520696, -0.5796146392822266], ["4c582015a7d976b0130cddee", "Wates House", 51.2420380341127, -0.5908584594726562], ["4b484917f964a520184b26e3", "The Rowbarge", 51.25055105804697, -0.5729025186239382], ["4bd86cb6e914a593c92f53fa", "The Wooden Bridge", 51.248547, -0.58514], ["4bd8b0442e6f0f4754240808", "The Seahorse", 51.218605041503906, -0.569018], ["4bb61c6bef159c74ff6b75f7", "The Astolat Public House", 51.23704061748392, -0.5893993377685547], ["4c978b274f16b71312c2ce3f", "The Queen Victoria", 51.21475338935852, -0.567119], ["4bb0eb08f964a520006a3ce3", "Anchor & Horseshoes", 51.254823, -0.548787], ["4c126e3c82a3c9b60ab0f9f8", "The Garage Tavern", 51.261162, -0.586647], ["4bed4208bac3c9b692fcfde9", "The Cricketers", 51.254246288978116, -0.6047425123927289], ["4b92cb0bf964a520501c34e3", "Horse & Groom", 51.24614672635736, -0.5279016494750977], ["4e8b7820be7b1b0656b1f927", "Apple Tree Pub", 51.2460676, -0.61427766], ["4dc58ce152b1e8f9f7d7378b", "Royal Oak", 51.248792, -0.626987], ["4c1a1c70838020a137aae661", "Withies Inn", 51.21199, -0.621551], ["4baded59f964a52031733be3", "The Jolly Farmer", 51.194106, -0.558244], ["4e21b6f052b1f82ffba120b5", "Compton Royal British Legion", 51.21349872454546, -0.6290990092597657], ["4b646071f964a52052ae2ae3", "The Jolly Farmer", 51.27866916930163, -0.5856227874755859], ["4d987f7961a3a1cd32aace42", "White Hart", 51.25011, -0.636736], ["4c8ccb34509e3704d9533655", "The Harrow Inn", 51.213387, -0.6316709518432617], ["4b71bb30f964a5200b592de3", "Bull's Head", 51.253260091783105, -0.5042177438735962], ["4f63b0b5e4b087553c2ae4fa", "The freeholders", 51.194189, -0.603889], ["4bc762a32f94d13aebd2117f", "The Cricketers", 51.194975, -0.608211], ["4be6c014bcef2d7f476805e5", "Worplesdon Place (Beef Eater Grill)", 51.27501810816803, -0.6078529357910156], ["4dbd63785da3ff58ec6192b1", "Scratchers", 51.19211, -0.60234], ["4e687a6bb3ad5d9197518ed6", "Three Lions - Shepherd Neame", 51.19198564344851, -0.6023865938186646], ["4c714ddcb5a5236acb995252", "The White heart Pub", 51.200254, -0.603593]]

我将这些结果传递给对它们进行评分的 API。我得到的结果是一个包含大量哈希的数组。键是array1中的ID,值是分数

array2 = [{"4bed4208bac3c9b692fcfde9"=>743.0}, {"4e21b6f052b1f82ffba120b5"=>789.0}, {"4b646071f964a52052ae2ae3"=>921.0}, {"4bb504a30ef1c9b6dbc2f412"=>99.0}, {"4b426369f964a520e3d225e3"=>80.0}, {"4c4f275651c2c9288af1859f"=>254.0}, {"4b92cb0bf964a520501c34e3"=>468.0}, {"4b425f85f964a52092d225e3"=>27.0}, {"4bd86cb6e914a593c92f53fa"=>512.0}, {"4e687a6bb3ad5d9197518ed6"=>622.0}, {"4b4447f8f964a5206cf325e3"=>73.0}, {"4b425ec9f964a52086d225e3"=>26.0}, {"4b484917f964a520184b26e3"=>328.0}, {"4b426047f964a520a4d225e3"=>37.0}, {"4c978b274f16b71312c2ce3f"=>253.0}, {"4b6de739f964a520769a2ce3"=>81.0}, {"4b48c8a8f964a520cb5626e3"=>167.0}, {"4bb475f449bdc9b65bcb0c10"=>80.0}, {"4c126e3c82a3c9b60ab0f9f8"=>739.0}, {"4bd8b0442e6f0f4754240808"=>210.0}, {"4bb61c6bef159c74ff6b75f7"=>231.0}, {"4b56243af964a5204f0228e3"=>56.0}, {"4b411f2bf964a52082c125e3"=>0.0}, {"4b48a60ff964a5208c5126e3"=>211.0}, {"4baded59f964a52031733be3"=>514.0}, {"4b40ce65f964a5205ebb25e3"=>124.0}, {"4b444648f964a52049f325e3"=>81.0}, {"4bb0eb08f964a520006a3ce3"=>376.0}, {"4f63b0b5e4b087553c2ae4fa"=>586.0}, {"4b9a86c4f964a520f2bd35e3"=>192.0}, {"4cb995490180721e03e09461"=>125.0}, {"4dc58ce152b1e8f9f7d7378b"=>955.0}, {"4b92c695f964a520aa1a34e3"=>20.0}, {"4c582015a7d976b0130cddee"=>484.0}, {"4c8ccb34509e3704d9533655"=>743.0}, {"4b48f87ff964a520096026e3"=>48.0}, {"4c1a1c70838020a137aae661"=>640.0}, {"4b894378f964a520e72632e3"=>55.0}, {"4e8b7820be7b1b0656b1f927"=>666.0}, {"4e02726dc65b8061424b59f1"=>78.0}, {"4b4261e4f964a520c6d225e3"=>57.0}, {"4ba55291f964a5209afa38e3"=>77.0}, {"4c714ddcb5a5236acb995252"=>473.0}, {"4b7451e5f964a520fad42de3"=>80.0}, {"4b490136f964a520a56126e3"=>71.0}, {"4d987f7961a3a1cd32aace42"=>1008.0}, {"4dbd63785da3ff58ec6192b1"=>622.0}, {"4b71bb30f964a5200b592de3"=>640.0}, {"4be6c014bcef2d7f476805e5"=>1016.0}, {"4bc762a32f94d13aebd2117f"=>577.0}]

我想最终得到一个新数组,其中包含数组 2 到数组 1 中匹配键的值,例如

["4c714ddcb5a5236acb995252", "The White heart Pub", 51.200254, -0.603593, 622]

并非 array1 中的每个值都会得到分数,有些不会得到任何结果,因此它们根本不会在 array2 中返回。所以我需要通过 id("4c714ddcb5a5236acb995252") 匹配它们并检查它们是否存在然后匹配。array1 中数据的分数

4

3 回答 3

2

Array.assoc这很好:

array2.each{|h|  array1.assoc(h.keys.first) << h.values.first}
p array1
于 2012-06-13T15:50:41.970 回答
0

我会像这样map使用Enumerable

array3 = array2.map do |item_from_array2|
  id = item_from_array2.keys[0]
  item_from_array1 = array1.find { |item| item.include?(id) }
  item_from_array1.dup << item_from_array2[id]
end

在这里,我枚举了 in 中的项目,array2以便结果仅包含array2. 然后,我得到 id 并在array1其中搜索具有该 id 的数组。然后,将 array2 中的值添加到从 array1 中找到的项目的末尾。最后,map根据每个返回值创建一个数组。

作为性能考虑,您可能希望从可以按 id 索引的第一个数组创建一个哈希,因为调用array1.find每次迭代都会比使用hash1[id]每次迭代慢。

于 2012-06-13T15:22:06.153 回答
0

由于这些应该是哈希,让我们将它们变成哈希:

h1 = array1.each_with_object({}) { |a, h| h[a.first] = a[1..-1] }
#=> {"4b411f2bf964a52082c125e3"=>["The Three Pigeons", 51.236318, -0.57055], ... }

h2 = array2.inject(:merge)
#=> {"4bed4208bac3c9b692fcfde9"=>743.0, ... }

然后我们可以很容易地创建一个最终的哈希:

result = h2.each { |k, v| h1[k] << v }
#=> {"4b411f2bf964a52082c125e3"=>["The Three Pigeons", 51.236318, -0.57055, 0.0], ... }

请注意,如果h2有一个h1还没有的键(或者,更具体地说,h1键的值不是数组),这将中断。

如果您真的希望在您提供的数组形式中使用它,您可以执行以下操作:

result = result.map { |k, v| v.unshift(k) }
#=> [["4b411f2bf964a52082c125e3", "The Three Pigeons", 51.236318, -0.57055, 0.0], ... ]
于 2012-06-13T15:38:18.837 回答