Greetings!
When assigning a value to an array as in the following, how could I replace the nil
s by 0
?
如下所示为数组赋值时,如何将nils替换为0?
array = [1,2,3]
array[10] = 2
array # => [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 2]
If not possible when assigning, how would I do it the best way afterwards? I thought of array.map { |e| e.nil? ? 0 : e }
, but well…
如果在分配时不可能,我将如何以最佳方式进行分配?我想到了array.map {| e | e.nil? ? 0:e},但是......
Thanks!
6 个解决方案
#1
6
There is no built-in function to replace nil
in an array, so yes, map
is the way to go. If a shorter version would make you happier, you could do:
没有内置函数来替换数组中的nil,所以是的,map是要走的路。如果较短的版本会让你更快乐,你可以这样做:
array.map {|e| e ? e : 0}
#2
12
To change the array after assignment:
要在分配后更改数组:
array.map! { |x| x || 0 }
Note that this also converts false
to 0
.
请注意,这也会将false转换为0。
If you want to use zeros during assignment, it's a little messy:
如果你想在任务期间使用零,那就有点乱了:
i = 10
a = [1, 2, 3]
a += ([0] * (i - a.size)) << 2
# => [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 2]
#3
1
To change the array in place
要更改阵列
array.map!{|x|x ?x:0}
If the array can contain false
you'll need to use this instead
如果数组可以包含false,则需要使用它
array.map!{|x|x.nil? ? 0:x}
#4
1
nil.to_i is 0, if all the numbers are integers then below should work. I think It is also the shortest answer here.
nil.to_i为0,如果所有数字都是整数,则下面应该有效。我认为这也是最简短的答案。
array.map!(&:to_i)
#5
1
a.select { |i| i }
This answer is too short so I am adding a few more words.
这个答案太短了,所以我补充说了几句话。
#6
0
Another approach would be to define your own function for adding a value to the array.
另一种方法是定义自己的函数,以便为数组添加值。
class Array
def addpad(index,newval)
concat(Array.new(index-size,0)) if index > size
self[index] = newval
end
end
a = [1,2,3]
a.addpad(10,2)
a => [1,2,3,0,0,0,0,0,0,0,2]
#1
6
There is no built-in function to replace nil
in an array, so yes, map
is the way to go. If a shorter version would make you happier, you could do:
没有内置函数来替换数组中的nil,所以是的,map是要走的路。如果较短的版本会让你更快乐,你可以这样做:
array.map {|e| e ? e : 0}
#2
12
To change the array after assignment:
要在分配后更改数组:
array.map! { |x| x || 0 }
Note that this also converts false
to 0
.
请注意,这也会将false转换为0。
If you want to use zeros during assignment, it's a little messy:
如果你想在任务期间使用零,那就有点乱了:
i = 10
a = [1, 2, 3]
a += ([0] * (i - a.size)) << 2
# => [1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 2]
#3
1
To change the array in place
要更改阵列
array.map!{|x|x ?x:0}
If the array can contain false
you'll need to use this instead
如果数组可以包含false,则需要使用它
array.map!{|x|x.nil? ? 0:x}
#4
1
nil.to_i is 0, if all the numbers are integers then below should work. I think It is also the shortest answer here.
nil.to_i为0,如果所有数字都是整数,则下面应该有效。我认为这也是最简短的答案。
array.map!(&:to_i)
#5
1
a.select { |i| i }
This answer is too short so I am adding a few more words.
这个答案太短了,所以我补充说了几句话。
#6
0
Another approach would be to define your own function for adding a value to the array.
另一种方法是定义自己的函数,以便为数组添加值。
class Array
def addpad(index,newval)
concat(Array.new(index-size,0)) if index > size
self[index] = newval
end
end
a = [1,2,3]
a.addpad(10,2)
a => [1,2,3,0,0,0,0,0,0,0,2]