如何在另一个数组中基于并行值对数据进行分组?

时间:2022-09-25 08:54:11

I am extracting data from a database and storing it in two arrays, one array contains the labels for the data, and the other contains the data. I need to manipulate the arrays in a way so that I have one label for each set of data.

我从数据库中提取数据并将其存储在两个数组中,一个数组包含数据的标签,另一个数组包含数据。我需要以某种方式操作数组,以便为每组数据都有一个标签。

Arrays right now

数组现在

labels - [Development, Development, Development, Development, Release, Release, Release, Release, ...]

标签-[开发、开发、开发、开发、发布、发布、发布、发布…]

data - [DevData1, DevData2, DevData3, DevData4, RelData1, RelData2, RelData3, RelData4, ...]

数据- [DevData1, DevData2, DevData3, DevData4, RelData1, RelData2, RelData3, RelData4,…]

I only need only label per set of data, but I want to break the data into segments to correspond with the single labels. There are four label/data pairs because this is data from the last four months. I would just split the array on every 4th element, but some of the months dont have entries. So I need a better way to split the data.

我只需要标签每组数据,但我想要将数据分割成段与单个标签对应。有四个标签/数据对,因为这是过去四个月的数据。我会把数组分成4个元素,但是有些月份没有条目。所以我需要一个更好的方法来分割数据。

Any ideas would be great. Thanks.

任何想法都很好。谢谢。

3 个解决方案

#1


4  

You could combine the separate arrays into a hash without too much effort:

您可以将独立的数组组合成散列,而无需付出太多努力:

a = [:Development, :Development, :Development, :Development, :Release, :Release, :Release, :Release]
b = [:DevData1, :DevData2, :DevData3, :DevData4, :RelData1, :RelData2, :RelData3, :RelData4]
h = Hash.new { |h, k| h[k] = [ ] }
a.each_with_index { |e, i| h[e].push(b[i]) }
# h is now {:Development=>[:DevData1, :DevData2, :DevData3, :DevData4], :Release=>[:RelData1, :RelData2, :RelData3, :RelData4]}

Just make sure you use Hash.new { |h,k| h[k] = [ ] } and not Hash.new([]) or you'll end up with two hash entries pointing at the same array and that will just make a mess of things.

确保使用散列。新的{|h,k| h[k] =[]},而不是Hash.new([]),否则您将会得到两个指向相同数组的散列条目,这将会使事情变得一团糟。

References:

引用:

#2


1  

Why not just store it as a hash?

为什么不把它存储为散列呢?

h = {"Development" => [DevData1, DevData2...], "Release" => [RelData1...]}

Then you could access "Development" data like this:

这样你就可以访问“开发”数据了:

h["Development"]
# => [DevData1, DevData2...]

#3


1  

A not very ruby solution to this would be just to loop through both of the arrays with an iterator and use them as you need them.

一个不是很好的ruby解决方案就是用迭代器循环遍历两个数组,并在需要时使用它们。

#1


4  

You could combine the separate arrays into a hash without too much effort:

您可以将独立的数组组合成散列,而无需付出太多努力:

a = [:Development, :Development, :Development, :Development, :Release, :Release, :Release, :Release]
b = [:DevData1, :DevData2, :DevData3, :DevData4, :RelData1, :RelData2, :RelData3, :RelData4]
h = Hash.new { |h, k| h[k] = [ ] }
a.each_with_index { |e, i| h[e].push(b[i]) }
# h is now {:Development=>[:DevData1, :DevData2, :DevData3, :DevData4], :Release=>[:RelData1, :RelData2, :RelData3, :RelData4]}

Just make sure you use Hash.new { |h,k| h[k] = [ ] } and not Hash.new([]) or you'll end up with two hash entries pointing at the same array and that will just make a mess of things.

确保使用散列。新的{|h,k| h[k] =[]},而不是Hash.new([]),否则您将会得到两个指向相同数组的散列条目,这将会使事情变得一团糟。

References:

引用:

#2


1  

Why not just store it as a hash?

为什么不把它存储为散列呢?

h = {"Development" => [DevData1, DevData2...], "Release" => [RelData1...]}

Then you could access "Development" data like this:

这样你就可以访问“开发”数据了:

h["Development"]
# => [DevData1, DevData2...]

#3


1  

A not very ruby solution to this would be just to loop through both of the arrays with an iterator and use them as you need them.

一个不是很好的ruby解决方案就是用迭代器循环遍历两个数组,并在需要时使用它们。