代码之家  ›  专栏  ›  技术社区  ›  Christian Stade-Schuldt

如何在Ruby中返回数组的一部分?

  •  91
  • Christian Stade-Schuldt  · 技术社区  · 15 年前

    使用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在数组中做所有的事情,所以我想知道是否有类似的事情。

    6 回复  |  直到 6 年前
        1
  •  143
  •   Paige Ruten    15 年前

    是的,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[5, 1]                #=> []
            a[5..10]               #=> []
    
        2
  •  23
  •   Raj    11 年前

    如果要拆分/剪切索引i上的数组,

    arr = arr.drop(i)
    
    > arr = [1,2,3,4,5]
     => [1, 2, 3, 4, 5] 
    > arr.drop(2)
     => [3, 4, 5] 
    
        3
  •  15
  •   Manuel    15 年前

    你可以使用 slice() 为此:

    >> 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]
    

    顺便说一下,据我所知,python“list”只是高效地实现了动态增长的数组。开始插入为O(n),结束插入为摊销O(1),随机访问为O(1)。

        4
  •  5
  •   user3449311    10 年前

    另一种方法是使用范围方法

    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]
    
        5
  •  0
  •   labyrinth    6 年前

    我喜欢这个范围:

    def first_half(list)
      list[0...(list.length / 2)]
    end
    
    def last_half(list)
      list[(list.length / 2)..list.length]
    end
    

    但是,要非常小心端点是否包含在您的范围内。在奇数长度的列表中,这一点非常重要,您需要在其中选择要打破中间的位置。否则,您将重复计算中间元素。

    上面的示例将一致地将中间元素放在最后一半。

        6
  •  -5
  •   Eric Duminil    8 年前

    你也可以这样写

    foo = [1,2,3,4,5,6,7]
    bar = [10,20,30,40,50,60,70]
    half = foo.length / 2
    foobar = (foo.first half) + (bar.drop half)
    
    => [1, 2, 3, 40, 50, 60, 70]