Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
惯用Ruby:数据结构转换_Ruby_Data Structures_Hash_Merge_Transformation - Fatal编程技术网

惯用Ruby:数据结构转换

惯用Ruby:数据结构转换,ruby,data-structures,hash,merge,transformation,Ruby,Data Structures,Hash,Merge,Transformation,进行以下数据结构转换的“rubyst”方法是什么: 我有 incoming = [ {:date => 20090501, :width => 2}, {:date => 20090501, :height => 7}, {:date => 20090501, :depth => 3}, {:date => 20090502, :width => 4},

进行以下数据结构转换的“rubyst”方法是什么:

我有

incoming = [ {:date => 20090501, :width => 2}, {:date => 20090501, :height => 7}, {:date => 20090501, :depth => 3}, {:date => 20090502, :width => 4}, {:date => 20090502, :height => 6}, {:date => 20090502, :depth => 2}, ] 传入=[{:date=>20090501,:width=>2}, {:日期=>20090501,:高度=>7}, {:date=>20090501,:depth=>3}, {:date=>20090502,:width=>4}, {:日期=>20090502,:高度=>6}, {:date=>20090502,:depth=>2}, ] 我想在日期之前把这些折叠起来,以

outgoing = [ {:date => 20090501, :width => 2, :height => 7, :depth => 3}, {:date => 20090502, :width => 4, :height => 6, :depth => 2}, ] 传出=[{:日期=>20090501,:宽度=>2,:高度=>7,:深度=>3}, {:日期=>20090502,:宽度=>4,:高度=>6,:深度=>2}, ] 在最后一步,如果列在每行中的顺序相同,那么数组数组也可以。另外,重要的是,我事先并不知道所有的散列键(也就是说,我不知道:宽度、高度或深度——它们可能是:猫、狗和:仓鼠)。

试试这个:

incoming = [ {:date => 20090501, :width => 2}, 
                 {:date => 20090501, :height => 7}, 
                 {:date => 20090501, :depth => 3}, 
                 {:date => 20090502, :width => 4}, 
                 {:date => 20090502, :height => 6}, 
                 {:date => 20090502, :depth => 2},
               ]

# Grouping by `:date`
temp = {}

incoming.each do |row|
    if temp[row[:date]].nil? 
        temp[row[:date]] = []
    end

    temp[row[:date]] << row
end      

# Merging it together
outcoming = []         

temp.each_pair do |date, hashlist|
    res = {}
    hashlist.each do |hash|
        res.merge!(hash)
    end
    outcoming << res 
end
试试这个:

incoming = [ {:date => 20090501, :width => 2}, 
                 {:date => 20090501, :height => 7}, 
                 {:date => 20090501, :depth => 3}, 
                 {:date => 20090502, :width => 4}, 
                 {:date => 20090502, :height => 6}, 
                 {:date => 20090502, :depth => 2},
               ]

# Grouping by `:date`
temp = {}

incoming.each do |row|
    if temp[row[:date]].nil? 
        temp[row[:date]] = []
    end

    temp[row[:date]] << row
end      

# Merging it together
outcoming = []         

temp.each_pair do |date, hashlist|
    res = {}
    hashlist.each do |hash|
        res.merge!(hash)
    end
    outcoming << res 
end
这里有一行:)

incoming.injection({}){o,i | o[i[:date]]| |=[];o[i[:date]]这里有一行代码:)

incoming.injection({})一个简洁的解决方案:

incoming = [ {:date => 20090501, :width => 2}, 
             {:date => 20090501, :height => 7}, 
             {:date => 20090501, :depth => 3}, 
             {:date => 20090502, :width => 4}, 
             {:date => 20090502, :height => 6}, 
             {:date => 20090502, :depth => 2},
           ]

temp = Hash.new {|hash,key| hash[key] = {}}
incoming.each {|row| temp[row[:date]].update(row)}
outgoing = temp.values.sort {|*rows| rows[0][:date] <=> rows[1][:date]}
incoming=[{:date=>20090501,:width=>2},
{:日期=>20090501,:高度=>7},
{:date=>20090501,:depth=>3},
{:date=>20090502,:width=>4},
{:日期=>20090502,:高度=>6},
{:date=>20090502,:depth=>2},
]
temp=Hash.new{| Hash,key{124; Hash[key]={}
incoming.each{| row | temp[row[:date]].update(row)}
outgoing=temp.values.sort{|*行|行[0][:日期]行[1][:日期]}
这里唯一棘手的是散列构造函数,它允许您提供一个在访问不存在的密钥时调用的块。因此,我让散列创建一个空散列,以便我们使用找到的值进行更新。然后我只使用日期作为散列键,按日期对散列值进行排序,然后进行转换。

se解决方案:

incoming = [ {:date => 20090501, :width => 2}, 
             {:date => 20090501, :height => 7}, 
             {:date => 20090501, :depth => 3}, 
             {:date => 20090502, :width => 4}, 
             {:date => 20090502, :height => 6}, 
             {:date => 20090502, :depth => 2},
           ]

temp = Hash.new {|hash,key| hash[key] = {}}
incoming.each {|row| temp[row[:date]].update(row)}
outgoing = temp.values.sort {|*rows| rows[0][:date] <=> rows[1][:date]}
incoming=[{:date=>20090501,:width=>2},
{:日期=>20090501,:高度=>7},
{:date=>20090501,:depth=>3},
{:date=>20090502,:width=>4},
{:日期=>20090502,:高度=>6},
{:date=>20090502,:depth=>2},
]
temp=Hash.new{| Hash,key{124; Hash[key]={}
incoming.each{| row | temp[row[:date]].update(row)}
outgoing=temp.values.sort{|*行|行[0][:日期]行[1][:日期]}

这里唯一需要技巧的是散列构造函数,它允许您提供一个在访问不存在的密钥时调用的块。因此,我让散列创建一个空散列,以便我们使用正在查找的值进行更新。然后我只使用日期作为散列密钥,按日期对散列值排序,然后进行转换。

如果使用g Ruby 1.8.7或Ruby 1.9+以下代码可读性良好:

incoming.group_by{|hash| hash[:date]}.map do |_, hashes| 
  hashes.reduce(:merge)
end
块属性(散列)中的下划线表示我们不需要/不关心该特定属性

#reduce是#inject的别名,用于将集合简化为单个项。在新的Ruby版本中,reduce还接受一个符号,即用于简化的方法的名称

它首先调用集合中第一项的方法,以第二项为参数,然后再次调用结果中的方法,以第三项为参数,依此类推,直到没有更多项为止

[1, 3, 2, 2].reduce(:+) => [4, 2, 2] => [6, 2] => 8

如果使用Ruby 1.8.7或Ruby 1.9+,则以下代码可读性良好:

incoming.group_by{|hash| hash[:date]}.map do |_, hashes| 
  hashes.reduce(:merge)
end
块属性(散列)中的下划线表示我们不需要/不关心该特定属性

#reduce是#inject的别名,用于将集合简化为单个项。在新的Ruby版本中,reduce还接受一个符号,即用于简化的方法的名称

它首先调用集合中第一项的方法,以第二项为参数,然后再次调用结果中的方法,以第三项为参数,依此类推,直到没有更多项为止

[1, 3, 2, 2].reduce(:+) => [4, 2, 2] => [6, 2] => 8

如果顺序很重要,那么就不要数组。依赖散列顺序是个坏主意,只能在Ruby 1.9中使用。是的,但是如果我保留散列结构,那么我的键将提供我需要的任何顺序。我只是对数组的顺序做了额外的注释,因为如果我有,就无法将其重新组合起来一个无序的子数组。如果顺序很重要,那么你需要数组。依赖哈希顺序是个坏主意,只在Ruby 1.9中有效。是的,但是如果我保留哈希结构,那么我的键将提供我需要的任何顺序。我只是对数组的顺序做了额外的注释,因为没有办法将其重新组合在一起之后,如果我有一个无序的子数组。解释:首先注入创建一个哈希,日期作为键,哈希数组作为值。然后映射将所有这些哈希合并为一个。解释:首先注入创建一个哈希,日期作为键,哈希数组作为值。然后映射将合并所有这些哈希一个。尼斯…不知道最近红宝石中的按功能分组。尼斯…不知道最近红宝石中的按功能分组。