Linux 如何将多级数组中的唯一值映射到 value=>array 的散列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3677217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I map unique values from a multi-level array to a hash of value=>array?
提问by Russ Bradberry
I have an array that looks something like this:
我有一个看起来像这样的数组:
[[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]]
What I would like to do is create an array of hashes that looks like this
我想做的是创建一个看起来像这样的哈希数组
[{"one" => [100,101,102]},
{"two" => [103]},
{"three" => [104,105]}]
The number portion will always be unique, the string portion will have duplicates. Every way I think about doing this I get some long function, I would like to know the "rails way" of going about this, I'm sure there's some obscure function I am missing.
数字部分将始终是唯一的,字符串部分将有重复。我想这样做的每一种方式我都会得到一些很长的功能,我想知道解决这个问题的“轨道方式”,我确定我缺少一些晦涩的功能。
采纳答案by Joseph Weissman
Not a Rails helper, but a common Ruby idiom can get you there. A little
不是 Rails 助手,但一个常见的 Ruby 习语可以帮助您实现目标。一点
arr.inject({}) { |h,(v,k)| h[k] ||= []; h[k] << v; h }
will do the trick.
会做的伎俩。
回答by shingara
There are no really function to do that. You need use inject and create yourself you hash.
没有真正的功能可以做到这一点。您需要使用注入并创建自己的哈希值。
回答by S?awosz
array = [[100, "one"], [101, "one"], [102, "one"], [103, "two"], [104, "three"], [105, "three"]]
h = Hash.new { |h,k| h[k] = []}
array.each { |a| h[a[1]] << a[0] }
回答by rfunduk
As shingara points out, this is pretty specific to the format of the array(s). You can do what you need like so:
正如 shingara 指出的那样,这非常特定于数组的格式。你可以像这样做你需要的:
a = [...your data...]
r = a.inject( {} ) do |h, el|
h[el.last] ||= []
h[el.last] << el.first
h
end
That gives a result like: {'one' => [101, 102], ... }
which is better than your request for an array of one-key hashes, IMO.
这给出了如下结果:{'one' => [101, 102], ... }
这比您对 IMO 单键哈希数组的请求要好。
回答by Chubas
Here's one implementation
这是一个实现
your_array.inject(Hash.new{|h,k| h[k] = []}) do |result, (a, b)|
result[b] << a
result
end
回答by mb14
If what you need is only group stuff then you can use the Rails group_by
function :
如果您只需要分组,那么您可以使用 Railsgroup_by
函数:
[[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]].group_by { |a| a[1] }
=> #<OrderedHash {"three"=>[[104, "three"], [105, "three"]],
"two"=>[[103, "two"]],
"one"=>[[100, "one"], [101, "one"], [102, "one"]]}
Not to far from what you need. So if you can use it as it stand, I guess that's fine, but if you need exaclty the format you said. I think it easier to do it yourself rather than usign this and converting.
离你需要的不远。因此,如果您可以按原样使用它,我想这很好,但是如果您需要精确地使用您所说的格式。我认为自己做比使用这个和转换更容易。
回答by J?rg W Mittag
Is there a specific reason why you need thatspecific format, i.e. an array of single-element hashes instead of just a bog-standard hash? Because in that case, it would literally be just
您需要这种特定格式是否有特定原因,即单元素散列数组而不是沼泽标准散列?因为在那种情况下,它实际上只是
arr.group_by(&:last)
回答by einarmagnus
The shortest way to get exactly what you ask for that I can come up with is:
得到你所要求的我能想出的最短方法是:
a = [[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]]
b = a.group_by(&:pop)
#=> {"three"=>[[104], [105]], "two"=>[[103]], "one"=>[[100], [101], [102]]}
which is probably what you want.
这可能是你想要的。
please note that a
gets ruined by this
请注意,这a
会被破坏
a
#=> [[100], [101], [102], [103], [104], [105]]
if that bothers you, you can write
如果这困扰你,你可以写
b = a.map(&:dup).group_by &:pop
instead.
反而。
And if you really want that format you wrote then you can add another map:
如果你真的想要你写的那种格式,那么你可以添加另一个地图:
b.map{|h,k| [h => k]}
#=> [{"one" => [100,101,102]}, {"two" => [103]}, {"three" => [104,105]}]
So to sum up:
所以总结一下:
[[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]].group_by(&:pop).map{ |h,k| [h => k] }