2014-06-25 64 views
-1

我有以下哈希。Ruby如何將平面數組轉換爲散列

@facet_counts = {"facet_queries"=>{}, 
"facet_fields"=> 
     {"product_collection_value"=>["traditional and imitation", 304, "chunky", 34, "modern", 15, "coloured gems", 12, "traditional", 0, "traditional & imitation", 0], 
     "product_material_value"=>["alloy", 161, "metal alloy", 132, "metal", 60, "925 sterling silver", 8, "lac", 3, "beads", 2, "beaded", 0, "brass", 0, "copper", 0, "crystal", 0, "fabric", 0, "feather", 0, "glass", 0, "jute", 0, "leather", 0, "pashmina", 0, "plastic", 0, "polymer beads", 0, "pu leather", 0, "rexin", 0, "rubber", 0, "satin", 0, "shell", 0, "silk", 0, "silk brocade", 0, "silver", 0, "silver alloy", 0, "stainless steel", 0, "sterling silver", 0, "stone", 0, "velvet", 0, "viscose", 0, "wood", 0, "wooden", 0, "wool", 0], 
     "product_type_value"=>["jhumkis", 364, "danglers", 53, "drops", 7, "hoops", 6, "victorian", 2, "armlets", 0, "bands", 0, "bangles", 0, "beaded", 0, "beads", 0, "bib", 0, "chains", 0, "charms", 0, "choker", 0, "clip on", 0, "cluster", 0, "cluster pendant necklaces", 0, "clusters", 0, "cocktail", 0, "contemporary", 0, "cuff", 0, "cz", 0, "diamond look", 0, "double chain", 0, "double fold", 0, "double strand", 0, "earrings", 0, "fashion", 0, "gemstone", 0, "hasli", 0, "hath phool", 0, "kada", 0, "kamarband", 0, "kundan", 0, "link", 0, "links", 0, "maang tika set", 0, "mangalsutras", 0, "modern", 0, "oxidised", 0, "oxidized", 0, "pair", 0, "pearl", 0, "pendant", 0, "pendant necklaces", 0, "potli", 0, "pouch", 0, "rani haar", 0, "rings", 0, "saree pins", 0, "set", 0, "single chain", 0, "single fold", 0, "single stone", 0, "single strand", 0, "singles", 0, "sling bag", 0, "strings", 0, "studded", 0, "studs", 0, "thewa", 0, "tote bag", 0, "traditional", 0, "with chain", 0, "with gemstone", 0, "without chain", 0, "without gemstone", 0], 
     "product_plating_value"=>["yellow gold plating", 135, "gold plating", 98, "silver", 39, "black silver", 15, "rhodium", 2, "white rhodium", 1, "14k yellow gold", 0, "18k yellow gold", 0, "alloy", 0, "black gold", 0, "black rhodium", 0, "brass", 0, "cubic zirconia", 0, "oxidised", 0, "pearl", 0, "rose gold", 0, "rose gold plating", 0, "silver plating", 0, "sterling silver", 0, "yellow gold", 0, "yellow rhodium", 0], 
     "product_gemstones_value"=>["cubic zirconia", 132, "pearl", 45, "semi-precious", 19, "crystal", 3, "precious", 3, "amethyst", 2, "citrine", 2, "garnet", 2, "peridot", 2, "green onyx", 1, "imitation kundan", 1, "iolite", 1, "aquamarine", 0, "black onyx", 0, "blue topaz", 0, "carnelian", 0, "chalcedony", 0, "coral", 0, "diamond", 0, "emerald", 0, "gem stones", 0, "green stone", 0, "howlite", 0, "hydro", 0, "jade", 0, "jasper", 0, "kundan", 0, "labradorite", 0, "lapis", 0, "lemon quartz", 0, "lemon stone", 0, "malachite", 0, "marcasite", 0, "moonstone", 0, "onyx", 0, "opal", 0, "pink amethyst", 0, "prehnite", 0, "quartz", 0, "rainbow", 0, "red onyx", 0, "red stone", 0, "red tiger eye", 0, "rhodolite", 0, "rose quartz", 0, "ruby", 0, "sapphire", 0, "smoky quartz", 0, "spinel", 0, "tanzanite", 0, "tiger eye", 0, "topaz", 0, "tourmaline", 0, "turquoise", 0, "white rainbow", 0, "white rainbow stone", 0], 
     "product_occasion_value"=>["special occasions or gifts", 358, "wedding or festive wear", 245, "everyday wear", 119, "work wear", 4, "religious", 0] 
    }, 
"facet_dates"=>{}, 
"facet_ranges"=>{}} 

我想散列對應頁面上的搜索結果。針對每個提交的名稱的數組指示具有該值的結果的值和數量。但由於這是一個數組,我無法輕鬆訪問計數。

什麼我目前做的是:

facet_fields=["product_collection_value","product_material_value","product_type_value","product_plating_value","product_gemstones_value","product_occasion_value"] 
    @count_hash = Hash.new 
    facet_fields.each {|field| 
     print @facet_counts["facet_fields"][field] 
     @facet_counts["facet_fields"][field].each_with_index{ |v,i| 
      if i%2 == 1 
       next 
      else 
       @count_hash[@facet_counts["facet_fields"][field][i]] = @facet_counts["facet_fields"][field][i+1] 
      end 
     } 

     print "\n\n" 
    } 

問題:這將創建一個新的Hash。但問題是,如果有多個相同標籤的條目。例如。 Modernproduct_collection_valueproduct_type_value中,所以值被覆蓋。有沒有一種方法可以轉換原始散列,以便輕鬆訪問計數?

+1

目前還不清楚是什麼你正在嘗試做的。發佈你的代碼並沒有幫助。而不是這樣做,解釋你想要什麼。 – sawa

+0

@sawa:謝謝你的建議。我已經相應地更新了這個問題。請仔細閱讀 – nish

+1

以下是一些寫作問題的提示:1.提供輸入數據。 (你已經這樣做了。)2.將輸入中的元素數量減少到問題所需的最小值。 3.縮短文本字符串,符號名稱等(「cv」而不是「產品收集值」)。 4.格式化,以避免需要水平滾動。 5.提供與輸入對應的所需輸出。 (通常這個問題可以從這一點來理解。) –

回答

1

的答案是: Hash[@facet_counts["facet_fields"].map { |k, v| [k, Hash[*v]] }]

1

我相信你正在嘗試做的是這樣的:

@count_hash = @facet_counts['facet_fields'].map do |k, v| 
    Hash[*v] 
end.inject({}) do |s, i| 
    s.merge(i) { |_, old, new| old + new } 
end 

第一部分,每個小字段映射到鍵值對的哈希值,例如 - 第一一直會是這樣的:

{"traditional and imitation"=>304, 
"chunky"=>34, "modern"=>15, "coloured gems"=>12, 
"traditional"=>0, "traditional & imitation"=>0} 

第二部分通過合計相同的鍵合併它們。你的例子的結果將是這個樣子:

{"traditional and imitation"=>304, "chunky"=>34, "modern"=>15, 
"coloured gems"=>12, "traditional"=>0, "traditional & imitation"=>0, 
"alloy"=>161, "metal alloy"=>132, "metal"=>60, "925 sterling silver"=>8, 
"lac"=>3, "beads"=>2, "beaded"=>0, "brass"=>0, "copper"=>0, "crystal"=>3, 
"fabric"=>0, "feather"=>0, "glass"=>0, "jute"=>0, "leather"=>0, "pashmina"=>0, 
"plastic"=>0, "polymer beads"=>0, "pu leather"=>0, "rexin"=>0, "rubber"=>0, 
"satin"=>0, "shell"=>0, "silk"=>0, "silk brocade"=>0, "silver"=>39, 
"silver alloy"=>0, "stainless steel"=>0, "sterling silver"=>0, ... }