设为首页 加入收藏

TOP

Ruby数组(2):数组方法详细整理(八)
2019-08-15 00:10:23 】 浏览:616
Tags:Ruby 数组 方法 详细 整理
2].product([]) # []
# 使用语句块形式
a = [1,2,3]
sub_a = a.product([4,5]) {|x| p x}
p sub_a

## 输出:
=begin
[1, 4]
[1, 5]
[2, 4]
[2, 5]
[3, 4]
[3, 5]
[1, 2, 3]
=end

rotate()和rotate!()

rotate(count=1) → new_ary
rotate!(count=1) → ary

转动数组,使得count位置处的元素作为新数组的第一个元素。带感叹号表示原处修改。

a = [ "a", "b", "c", "d" ]
a.rotate         # ["b", "c", "d", "a"]
a                # ["a", "b", "c", "d"]
a.rotate(2)      # ["c", "d", "a", "b"]
a.rotate(-3)     # ["b", "c", "d", "a"]

a = [ "a", "b", "c", "d" ]
a.rotate!        # ["b", "c", "d", "a"]
a                # ["b", "c", "d", "a"]
a.rotate!(2)     # ["d", "a", "b", "c"]
a.rotate!(-3)    # ["a", "b", "c", "d"]

transpos()

transpose → new_ary

如果是多维数组,则返回行列转换后的新数组。如果元素个数不一致,则直接报错。

a = [[1,2], [3,4], [5,6]]
a.transpose   # [[1, 3, 5], [2, 4, 6]]

[[1,2,3],[3,4],[5,6]].transpose # IndexError

simple()

sample → obj
sample(random: rng) → obj
sample(n) → new_ary
sample(n, random: rng) → new_ary

从数组中随机选择一个或n个元素。选择随机元素的方式是使用随机的索引进行选取。如果选择多个随机元素,则选择随机元素的索引位置会保证唯一性,但仍然可能会选中重复元素,因为数组自身可能会包含重复元素。

参数rng表示指定生成索引随机数的生成器。

当为空数组时,第一种形式返回nil,第二种形式返回空数组。

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.sample         # 7
a.sample(4)      # [6, 4, 2, 5]
a.sample(random: Random.new(1))     # 6
a.sample(4, random: Random.new(1))  # [6, 10, 9, 2]

shuffle()和shuffle!()

shuffle → new_ary
shuffle(random: rng) → new_ary

shuffle! → ary
shuffle!(random: rng) → ary

打乱数组元素并返回。感叹号后缀表示原处修改。

rng参数指定生成随机数的生尘器。

# shuffle()
a = [ 1, 2, 3 ]           # [1, 2, 3]
a.shuffle                 # [2, 3, 1]
a                         # [1, 2, 3]

a.shuffle(random: Random.new(1))  # [1, 3, 2]

# shuffle!()
a = [ 1, 2, 3 ]           # [1, 2, 3]
a.shuffle!                # [2, 3, 1]
a                         # [2, 3, 1]

a.shuffle!(random: Random.new(1))  # [1, 3, 2]

sum()

sum(init=0) → number
sum(init=0) {|e| expr } → number

返回各元素加法操作之和。例如[e1, e2, e3].sum返回init + e1 + e2 + e3

如果使用了语句块,则在相加之前,先对各元素执行语句块。

如果数组为空,则返回init参数值。

[].sum                             # 0
[].sum(0.0)                        # 0.0
[1, 2, 3].sum                      # 6
[3, 5.5].sum                       # 8.5
[2.5, 3.0].sum(0.0) {|e| e * e }   # 15.25
[Object.new].sum                   # TypeError

不仅限于可以执行数值相加操作,只要通过init参数显式指明初始值类型即可:

["a", "b", "c"].sum("")            # "abc"
[[1], [[2]], [3]].sum([])          # [1, [2], 3]

但是,join()flatten()都比上述方式运行的更快。

["a", "b", "c"].join               # "abc"
[[1], [[2]], [3]].flatten(1)       # [1, [2], 3]

reverse()和reverse!()

reverse → new_ary
reverse! → ary

将数组元素反转。带感叹号表示原处反转。

# reverse()
["a","b","c"].reverse   # ["c","b", a"]
[1].reverse             # [1]

# reverse!()
a = ["a","b","c"]
a.reverse!       # ["c","b","a"]
a                # ["c","b","a"]

reverse_each()

reverse_each {|item| block} → ary
reverse_each → Enumerator

类似于each(),但反向迭代,即从数组尾部开始迭代。

a = [ "a", "b", "c" ]
a.reverse_each {|x| print x," "} # 输出c b a

sort()和sort!()

sort → new_ary
sort {|a, b| block} → new_ary

sort! → ary
sort! {|a, b| block} → ary

对数组元素进行排序,然后返回。带感叹号后缀表示原处修改。

当没有使用语句块时,排序

首页 上一页 5 6 7 8 9 下一页 尾页 8/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Ruby中to_s和to_str、to_i和to_in.. 下一篇Python之路【第四篇】:Python基础..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目