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 に渡します。返される結果は、多くのハッシュを含む配列です。Key は 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

私は次のようにEnumerablemapから使用します:

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

ここでは、のアイテムを列挙しarray2て、結果にに存在​​するアイテムのみが含まれるようにしarray2ます。次に、IDを取得し、そのarray1IDを持つ配列を検索します。次に、array2の値を、array1で見つかったアイテムの最後に追加します。最後に、mapすべての戻り値から配列を作成します。

array1.findパフォーマンスの考慮事項として、すべての反復の呼び出しはすべての反復を使用するよりも遅くなるため、IDでインデックスを作成できる最初の配列からハッシュを作成することをお勧めし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 に答える