如何在Ruby中返回数组的一部分?
使用Python中的列表,我可以使用以下代码返回其中的一部分:如何在Ruby中返回数组的一部分?,ruby,arrays,list,slice,Ruby,Arrays,List,Slice,使用Python中的列表,我可以使用以下代码返回其中的一部分: foo = [1,2,3,4,5,6] bar = [10,20,30,40,50,60] half = len(foo) / 2 foobar = foo[:half] + bar[half:] 由于Ruby在阵列中完成所有操作,我想知道是否有类似的功能。您可以使用: >> foo = [1,2,3,4,5,6] => [1, 2, 3, 4, 5, 6] >> bar = [10,20,30,40
foo = [1,2,3,4,5,6]
bar = [10,20,30,40,50,60]
half = len(foo) / 2
foobar = foo[:half] + bar[half:]
由于Ruby在阵列中完成所有操作,我想知道是否有类似的功能。您可以使用:
>> foo = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
>> bar = [10,20,30,40,50,60]
=> [10, 20, 30, 40, 50, 60]
>> half = foo.length / 2
=> 3
>> foobar = foo.slice(0, half) + bar.slice(half, foo.length)
=> [1, 2, 3, 40, 50, 60]
def first_half(list)
list[0...(list.length / 2)]
end
def last_half(list)
list[(list.length / 2)..list.length]
end
顺便说一句,据我所知,Python“列表”只是高效地实现了动态增长的数组。开头的插入是O(n),结尾的插入是摊销O(1),随机访问是O(1)。是的,Ruby的数组切片语法与Python非常相似。以下是数组索引方法的
ri
文档:
--------------------------------------------------------------- Array#[]
array[index] -> obj or nil
array[start, length] -> an_array or nil
array[range] -> an_array or nil
array.slice(index) -> obj or nil
array.slice(start, length) -> an_array or nil
array.slice(range) -> an_array or nil
------------------------------------------------------------------------
Element Reference---Returns the element at index, or returns a
subarray starting at start and continuing for length elements, or
returns a subarray specified by range. Negative indices count
backward from the end of the array (-1 is the last element).
Returns nil if the index (or starting index) are out of range.
a = [ "a", "b", "c", "d", "e" ]
a[2] + a[0] + a[1] #=> "cab"
a[6] #=> nil
a[1, 2] #=> [ "b", "c" ]
a[1..3] #=> [ "b", "c", "d" ]
a[4..7] #=> [ "e" ]
a[6..10] #=> nil
a[-3, 3] #=> [ "c", "d", "e" ]
# special cases
a[5] #=> nil
a[6, 1] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
如果要拆分/剪切索引i上的数组
arr = arr.drop(i)
> arr = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
> arr.drop(2)
=> [3, 4, 5]
另一种方法是使用范围方法
foo = [1,2,3,4,5,6]
bar = [10,20,30,40,50,60]
a = foo[0...3]
b = bar[3...6]
print a + b
=> [1, 2, 3, 40, 50 , 60]
我喜欢这个范围:
>> foo = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
>> bar = [10,20,30,40,50,60]
=> [10, 20, 30, 40, 50, 60]
>> half = foo.length / 2
=> 3
>> foobar = foo.slice(0, half) + bar.slice(half, foo.length)
=> [1, 2, 3, 40, 50, 60]
def first_half(list)
list[0...(list.length / 2)]
end
def last_half(list)
list[(list.length / 2)..list.length]
end
但是,要非常小心端点是否包含在范围内。这在奇数长度的列表中变得至关重要,您需要选择在哪里打破中间。否则,您将重复计算中间元素
上面的示例将始终将中间元素放在最后一半。Ruby 2.6无起点/无止境范围
(..1)
# or
(...1)
(1..)
# or
(1...)
ROLES = %w[superadmin manager admin contact user]
ROLES[ROLES.index('admin')..]
=> ["admin", "contact", "user"]
您的意思是在第二个切片中使用条形数组吗?仅供参考:
slice!()
不会就地修改数组,而是“删除索引(可选长度元素)或范围给定的元素。”per@joshuapinter谢谢你!我只是被这一点(显然是再次)打断了。为什么[5,1]与[6,1]不同?@dertoni:a[2..-1]
从第三个元素到最后一个元素a[2…-1]
从第三个元素到最后第二个元素。@Rafeh干杯,一直在想这个垃圾是怎么工作的,-1做了trick@CleverProgrammer语法比公认的答案更接近Python。我喜欢Ruby,但我必须说Python的语法更短更清晰。另外,还可以指定步骤:范围(10)[:5:-1]
如果需要,可以使用(list.length/2.0).ceil
将中间元素始终放在前半部分。