I'm trying to learn how to search within a two-dimensional array; for example:
我正在尝试学习如何在二维数组中搜索;例如:
array = [[1,1], [1,2], [1,3], [2,1], [2,4], [2,5]]
I want to know how to search within the array for the arrays that are of the form [1, y]
and then show what the other y
numbers are: [1, 2, 3]
.
我想知道如何在数组中搜索形式为[1,y]的数组,然后显示其他y数字是什么:[1,2,3]。
If anyone can help me understand how to search only with numbers (as a lot of the examples I found include strings or hashes) and even where to look for the right resources even, that would be helpful.
如果有人可以帮助我理解如何只搜索数字(因为我发现很多例子包括字符串或哈希),甚至在哪里寻找合适的资源,这将是有帮助的。
5 个解决方案
#1
5
Ruby allows you to look into an element by using parentheses in the block argument. select
and map
only assign a single block argument, but you can look into the element:
Ruby允许您通过在块参数中使用括号来查看元素。 select和map只分配一个块参数,但您可以查看该元素:
array.select{|(x, y)| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|(x, y)| x == 1}.map{|(x, y)| y}
# => [1, 2, 3]
You can omit the parentheses that correspond to the entire expression between |...|
:
您可以省略与| ... |之间的整个表达式对应的括号:
array.select{|x, y| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|x, y| x == 1}.map{|x, y| y}
# => [1, 2, 3]
As a coding style, it is a custom to mark unused variables as _
:
作为编码样式,将未使用的变量标记为_是自定义:
array.select{|x, _| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|x, _| x == 1}.map{|_, y| y}
# => [1, 2, 3]
#2
6
You can use Array#select and Array#map methods:
您可以使用Array#select和Array#map方法:
array = [[1,1], [1,2], [1,3], [2,1], [2,4], [2,5]]
#=> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 4], [2, 5]]
array.select { |el| el[0] == 1 }
#=> [[1, 1], [1, 2], [1, 3]]
array.select { |el| el[0] == 1 }.map {|el| el[1] }
#=> [1, 2, 3]
For more methods on arrays explore docs.
有关阵列的更多方法,请探索文档。
#3
5
If you first select and then map you can use the grep function to to it all in one function:
如果你先选择然后映射你可以使用grep函数在一个函数中使用它:
p array.grep ->x{x[0]==1}, &:last #=> [1,2,3]
#4
1
Another way of doing the same thing is to use Array#map together with Array#compact. This has the benefit of only requiring one block and a trivial operation, which makes it a bit easier to comprehend.
另一种做同样事情的方法是将Array#map与Array#compact一起使用。这样做的好处是只需要一个块和一个简单的操作,这使得它更容易理解。
array.map { |a, b| a if b == 1 }
#=> [1, 2, 3, nil, nil, nil]
array.map { |a, b| a if b == 1 }.compact
#=> [1, 2, 3]
#5
1
You can use each_with_object
:
你可以使用each_with_object:
array.each_with_object([]) { |(x, y), a| a << y if x == 1 }
#=> [1, 2, 3]
#1
5
Ruby allows you to look into an element by using parentheses in the block argument. select
and map
only assign a single block argument, but you can look into the element:
Ruby允许您通过在块参数中使用括号来查看元素。 select和map只分配一个块参数,但您可以查看该元素:
array.select{|(x, y)| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|(x, y)| x == 1}.map{|(x, y)| y}
# => [1, 2, 3]
You can omit the parentheses that correspond to the entire expression between |...|
:
您可以省略与| ... |之间的整个表达式对应的括号:
array.select{|x, y| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|x, y| x == 1}.map{|x, y| y}
# => [1, 2, 3]
As a coding style, it is a custom to mark unused variables as _
:
作为编码样式,将未使用的变量标记为_是自定义:
array.select{|x, _| x == 1}
# => [[1, 1], [1, 2], [1, 3]]
array.select{|x, _| x == 1}.map{|_, y| y}
# => [1, 2, 3]
#2
6
You can use Array#select and Array#map methods:
您可以使用Array#select和Array#map方法:
array = [[1,1], [1,2], [1,3], [2,1], [2,4], [2,5]]
#=> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 4], [2, 5]]
array.select { |el| el[0] == 1 }
#=> [[1, 1], [1, 2], [1, 3]]
array.select { |el| el[0] == 1 }.map {|el| el[1] }
#=> [1, 2, 3]
For more methods on arrays explore docs.
有关阵列的更多方法,请探索文档。
#3
5
If you first select and then map you can use the grep function to to it all in one function:
如果你先选择然后映射你可以使用grep函数在一个函数中使用它:
p array.grep ->x{x[0]==1}, &:last #=> [1,2,3]
#4
1
Another way of doing the same thing is to use Array#map together with Array#compact. This has the benefit of only requiring one block and a trivial operation, which makes it a bit easier to comprehend.
另一种做同样事情的方法是将Array#map与Array#compact一起使用。这样做的好处是只需要一个块和一个简单的操作,这使得它更容易理解。
array.map { |a, b| a if b == 1 }
#=> [1, 2, 3, nil, nil, nil]
array.map { |a, b| a if b == 1 }.compact
#=> [1, 2, 3]
#5
1
You can use each_with_object
:
你可以使用each_with_object:
array.each_with_object([]) { |(x, y), a| a << y if x == 1 }
#=> [1, 2, 3]