【问题标题】:Hash of hashes, how to get keys of the first nested hash散列的散列,如何获取第一个嵌套散列的键
【发布时间】:2013-07-08 02:21:45
【问题描述】:

我在 Ruby 中使用了一个哈希值,称为 MYMOVIES,如下所示。

    {"127 Hours"=>
       {"title"=>"127 Hours",
       "year"=>"2010",
       "plays"=>1,
       "last_played"=>1300489200,
       "seen_date"=>"19/3/2011",
       "imdb_id"=>"tt1542344",
       "rating"=>"6",
       "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"},
    "Zombieland"=>
       {"title"=>"Zombieland",
       "year"=>"2009",
       "plays"=>1,
       "last_played"=>1290207600,
       "seen_date"=>"20/11/2010",
       "imdb_id"=>"tt1156398",
       "rating"=>"7",
       "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}}

现在,我想获取第一个嵌套哈希的所有键(即标题、年份、播放量、...、omdbapiurl)。

我试过了:

   mynestedhash = MYMOVIES.first
   puts mynestedhash.keys.to_s

但我得到了错误:

    undefined method `keys' for #<Array:0x801c56f8> (NoMethodError)

我该怎么办?

【问题讨论】:

    标签: ruby hash nested


    【解决方案1】:

    如果所有内部哈希具有相同的键,则以下就足够了

    first_outer_key, first_outer_value = MYMOVIES.first 
    first_inner_hash = first_outer_value # change name to show what we have
    inner_keys = first_inner_hash.keys
    

    如果内部哈希的键可以不同,你应该加入它们 就像 Priti 和 toro2k 在他们的解决方案中所做的那样。

    【讨论】:

      【解决方案2】:

      应该这样做:

      MYMOVIES.map { |_, h| h.keys }.flatten.uniq
      # => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 
      

      您的代码不起作用,因为方法 first 返回一个数组,而不是哈希:

      MYMOVIES.first
      # => ["127 Hours", {"title"=>"127 Hours", ... }]]
      

      更新如果你想得到第一个哈希的键,那么你可以这样做:

      nested_hash = MYMOVIES.first[1]
      nested_hash.keys
      # => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 
      

      或者:

      _, nested_hash = MYMOVIES.first
      nested_hash.keys
      # => ["title", "year", "plays", "last_played", "seen_date", "imdb_id", "rating", "omdbapiurl"] 
      

      【讨论】:

      • 是的,但是没有任何方法可以从散列中返回散列(因为我将它创建为散列的散列!)? mytmplist = {} mytmplist['title'] = "" mytmplist['year'] = "" mytmplist['plays'] = 1 mytmplist['last_played'] = mytimestamp mytmplist['seen_date'] = myformattedseendate mytmplist['imdb_id '] = "" mytmplist['rating'] = "" mytmplist['omdbapiurl'] = "" MYMOVIES[movietitle] = ""
      • @MDT 你在这篇文章中提出的问题已经得到解答。现在,如果您有其他需要,请在另一个问题中发布。从评论中看不到您要查找的内容。
      • mytmplist = {} mytmplist['title'] = "" mytmplist['year'] = "" mytmplist['plays'] = 1 mytmplist['last_played'] = mytimestamp mytmplist['seen_date'] = myformattedseendate mytmplist['imdb_id'] = "" mytmplist['rating'] = "" mytmplist['omdbapiurl'] = "" MYMOVIES[movietitle] = mytmplist 这就是我想发布的内容,抱歉之前的帖子无法阅读,非常感谢您的有用回答。
      • 非常感谢,我知道了! :)
      【解决方案3】:
      require 'pp'
      h = {"127 Hours"=>
             {"title"=>"127 Hours",
             "year"=>"2010",
             "plays"=>1,
             "last_played"=>1300489200,
             "seen_date"=>"19/3/2011",
             "imdb_id"=>"tt1542344",
             "rating"=>"6",
             "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"},
          "Zombieland"=>
             {"title"=>"Zombieland",
             "year"=>"2009",
             "plays"=>1,
             "last_played"=>1290207600,
             "seen_date"=>"20/11/2010",
             "imdb_id"=>"tt1156398",
             "rating"=>"7",
             "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}}
      
      pp h.flat_map{|k,v| v.keys}.uniq
      

      输出

      ["title",
       "year",
       "plays",
       "last_played",
       "seen_date",
       "imdb_id",
       "rating",
       "omdbapiurl"]
      

      现在看看为什么你的代码在下面不起作用:

      h = {"127 Hours"=>
             {"title"=>"127 Hours",
             "year"=>"2010",
             "plays"=>1,
             "last_played"=>1300489200,
             "seen_date"=>"19/3/2011",
             "imdb_id"=>"tt1542344",
             "rating"=>"6",
             "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"},
          "Zombieland"=>
             {"title"=>"Zombieland",
             "year"=>"2009",
             "plays"=>1,
             "last_played"=>1290207600,
             "seen_date"=>"20/11/2010",
             "imdb_id"=>"tt1156398",
             "rating"=>"7",
             "omdbapiurl"=>"http://www.omdbapi.com/?t=Zombieland&y=2009"}}
      
      h.first
      #["127 Hours",
      # {"title"=>"127 Hours",
      # "year"=>"2010",
      # "plays"=>1,
      # "last_played"=>1300489200,
      # "seen_date"=>"19/3/2011",
      # "imdb_id"=>"tt1542344",
      # "rating"=>"6",
      # "omdbapiurl"=>"http://www.omdbapi.com/?t=127 Hours&y=2010"}]
      p h.first.grep /keys/
      #[]
      

      现在很明显,从#grep 方法,Array 没有keys 方法。所以试试上面的代码,让它可以工作。

      【讨论】:

      • 非常感谢您的有用解释!
      猜你喜欢
      • 2021-11-03
      • 2013-03-14
      • 1970-01-01
      • 2018-11-07
      • 2014-07-28
      • 2013-04-14
      • 2014-09-13
      • 2014-02-20
      • 2016-09-04
      相关资源
      最近更新 更多