如何根据数组中特定的索引倍数进行选择

时间:2022-07-22 21:16:33

This is probably very basic question.

这可能是非常基本的问题。

Suppose I have an array with 19 items [0,1,2,3,4..19]. How could I selecting only the ones with the index count that are multiples of a given number i.e. 2?

假设我有一个包含19个项目的数组[0,1,2,3,4..19]。我怎样才能只选择索引计数是给定数字的倍数的那些,即2?

Updated: Supposed this code is intended to use for columns. How to manage to get indexes that would be of first column given that all are multiple of one?

更新:假设此代码旨在用于列。如何设置获得第一列的索引,并且所有索引都是多个索引?

4 个解决方案

#1


2  

Select elements which are multiples of a given number (works if your elements equal the index):

选择给定数字的倍数的元素(如果元素等于索引,则有效):

ary.select { |element| element % 2 == 0 }

In this special case, you can also use symbol to proc:

在这种特殊情况下,您还可以使用符号来处理:

ary.select &:even?

If your elements are different from the indices, group in pairs of 2 and use the first element:

如果您的元素与索引不同,则成对分组为2并使用第一个元素:

ary.each_slice(2).map { |slice| slice[0] }

#2


2  

Check out Enumerable#select (Array includes Enumerable). You can do this:

查看Enumerable #select(Array包括Enumerable)。你可以这样做:

1.9.3p392 :001 > a = [1,2,3,4,5,6]
 => [1, 2, 3, 4, 5, 6]
1.9.3p392 :002 > a.select {|n| n % 2 == 0}
 => [2, 4, 6]

select will filter the array, picking out anything for which the block returns true. In this case, using the mod (%) operator to find elements divisible by 2.

select将过滤数组,挑选出块返回true的任何内容。在这种情况下,使用mod(%)运算符查找可被2整除的元素。

#3


1  

If I understand you correctly you want to take only the even indices. Try using each_index.

如果我理解你,你只想采用偶数指数。尝试使用each_index。

arr = (0..19).to_a
arr.each_index { |x| puts arr[x] if x % 2 == 0 }

This prints:

这打印:

0
2
4
6
8
10
12
14
16
18

If you want the even elements of the array you could use find_all too.

如果你想要数组的偶数元素,你也可以使用find_all。

arr.find_all { |e| e.even? }

#4


0  

(1..19).to_a.select{|e| e.%(2).zero?}

This is slightly faster than using == 0.

这比使用== 0稍快。

#1


2  

Select elements which are multiples of a given number (works if your elements equal the index):

选择给定数字的倍数的元素(如果元素等于索引,则有效):

ary.select { |element| element % 2 == 0 }

In this special case, you can also use symbol to proc:

在这种特殊情况下,您还可以使用符号来处理:

ary.select &:even?

If your elements are different from the indices, group in pairs of 2 and use the first element:

如果您的元素与索引不同,则成对分组为2并使用第一个元素:

ary.each_slice(2).map { |slice| slice[0] }

#2


2  

Check out Enumerable#select (Array includes Enumerable). You can do this:

查看Enumerable #select(Array包括Enumerable)。你可以这样做:

1.9.3p392 :001 > a = [1,2,3,4,5,6]
 => [1, 2, 3, 4, 5, 6]
1.9.3p392 :002 > a.select {|n| n % 2 == 0}
 => [2, 4, 6]

select will filter the array, picking out anything for which the block returns true. In this case, using the mod (%) operator to find elements divisible by 2.

select将过滤数组,挑选出块返回true的任何内容。在这种情况下,使用mod(%)运算符查找可被2整除的元素。

#3


1  

If I understand you correctly you want to take only the even indices. Try using each_index.

如果我理解你,你只想采用偶数指数。尝试使用each_index。

arr = (0..19).to_a
arr.each_index { |x| puts arr[x] if x % 2 == 0 }

This prints:

这打印:

0
2
4
6
8
10
12
14
16
18

If you want the even elements of the array you could use find_all too.

如果你想要数组的偶数元素,你也可以使用find_all。

arr.find_all { |e| e.even? }

#4


0  

(1..19).to_a.select{|e| e.%(2).zero?}

This is slightly faster than using == 0.

这比使用== 0稍快。