博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ruby 数组操作(和 Python3 列表+元组+集合 对比)
阅读量:4229 次
发布时间:2019-05-26

本文共 29984 字,大约阅读时间需要 99 分钟。

本来是想写到 Ruby 学习笔记一块的,但是数组操作这块的内容也太多了,所以单独提取出来了。Ruby 里没有单独的集合的概念,或许是因为数组就已经足够涵盖了这些了。所以,话不多说,直接上菜(为了对比明显和更加简洁,Ruby 中尽量去掉括号)!

数组内建方法

# Ruby[20] pry(main)> names = Array.new=> [][22] pry(main)> names = Array.new(10)=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil][23] pry(main)> names = Array.new 10=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil][24] pry(main)> names.length=> 10[25] pry(main)> names.size=> 10[27] pry(main)> nums = Array.[](1, 2, 3, 4,5)=> [1, 2, 3, 4, 5][28] pry(main)> nums = Array[1, 2, 3, 4,5]=> [1, 2, 3, 4, 5]
# Python3>>> names = list()>>> names[]>>> names = [None] * 10>>> names[None, None, None, None, None, None, None, None, None, None]>>> len(names)10>>> nums = list([1, 2, 3, 4, 5])>>> nums[1, 2, 3, 4, 5]>>> nums = [1, 2, 3, 4, 5]>>> nums[1, 2, 3, 4, 5]

默认值

# Ruby[26] pry(main)> names = Array.new(10, "hello")=> ["hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello", "hello"]
# Python3>>> names = ["hello"] * 10>>> names['hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello']

array to str

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][116] pry(main)> hello.join ""=> "hello"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> "".join(hello)'hello'

array & other_array

# Ruby[29] pry(main)> hello = Array(['h', 'e', 'l', 'l', 'o'])=> ["h", "e", "l", "l", "o"][30] pry(main)> world = Array(['w', 'o', 'r', 'l', 'd'])=> ["w", "o", "r", "l", "d"][31] pry(main)> hello & world=> ["l", "o"]
# Python3>>> hello = list("hello")>>> hello['h', 'e', 'l', 'l', 'o']>>> world = list("world")>>> world['w', 'o', 'r', 'l', 'd']>>> set(hello) & set(world){'l', 'o'}>>> set(hello).intersection(set(world)){'l', 'o'}

array * int [or] array * str

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][35] pry(main)> hello * 2=> ["h", "e", "l", "l", "o", "h", "e", "l", "l", "o"][38] pry(main)> hello * "2"=> "h2e2l2l2o"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello * 2['h', 'e', 'l', 'l', 'o', 'h', 'e', 'l', 'l', 'o']>>> "2".join(hello)'h2e2l2l2o'

array + other_array

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][39] pry(main)> hello + world=> ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello + world['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

array - other_array

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][40] pry(main)> hello - world=> ["h", "e"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> list(set(hello) - set(world))['e', 'h']

array | other_array

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][41] pry(main)> hello | world=> ["h", "e", "l", "o", "w", "r", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> list(set(hello) | set(world))['w', 'e', 'r', 'l', 'o', 'h', 'd']

array << obj

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][45] pry(main)> hello << world=> ["h", "e", "l", "l", "o", ["w", "o", "r", "l", "d"]]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.append(world)>>> hello['h', 'e', 'l', 'l', 'o', ['w', 'o', 'r', 'l', 'd']]

array <=> other_array

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][16] pry(main)> hello <=> world=> -1
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']

array == other_array 

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][17] pry(main)> hello == world=> false[18] pry(main)> hello == hello=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello == worldFalse>>> hello == helloTrue

array[index] (array[range])

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][19] pry(main)> hello[1]=> "e"[20] pry(main)> hello[1, 3]=> ["e", "l", "l"][21] pry(main)> hello[1..3]=> ["e", "l", "l"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[1]'e'>>> hello[1: 4]['e', 'l', 'l']>>> hello[1: -1]['e', 'l', 'l']

array[index] = obj (array[range] = obj)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][22] pry(main)> hello[1] = 'o'=> "o"[23] pry(main)> hello[1..3] = [1, 2, 3]=> [1, 2, 3][24] pry(main)> hello=> ["h", 1, 2, 3, "o"][62] pry(main)> hello[1..3] = (1..3).map{|item | item * 2}=> [2, 4, 6][63] pry(main)> hello=> ["h", 2, 4, 6, "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[1] = 'o'>>> hello['h', 'o', 'l', 'l', 'o']>>> hello[1:4] = range(1, 4)>>> hello['h', 1, 2, 3, 'o']>>> hello[1:4] = [2*item for item in range(1, 4)]>>> hello['h', 2, 4, 6, 'o']

array.all? [or] array.all?{|item| block}

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][10] pry(main)> hello.all?=> true[11] pry(main)> hello.all?{|item| item > 'l'}=> false[12] pry(main)> hello.all?{|item| item > 'a'}=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> all(hello)True>>> all([item > 'l' for item in hello])False>>> all([item > 'a' for item in hello])True

array.any? [or] array.any?{|item| block}

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][13] pry(main)> hello.any?=> true[14] pry(main)> hello.any?{|item| item > 'a'}=> true[15] pry(main)> hello.any?{|item| item > 'z'}=> false
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> any(hello)True>>> any([item > 'a' for item in hello])True>>> any([item > 'z' for item in hello])False

array.at(index)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][49] pry(main)> hello.at(3)=> "l"[50] pry(main)> hello.at 3=> "l"[51] pry(main)> hello[3]=> "l"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[3]'l'

array.clear

# Ruby[52] pry(main)> hello=> ["h", "e", "l", "l", "o"][53] pry(main)> hello.clear=> []
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.clear()>>> hello[]

array.compact (array.compact!)

# Ruby[83] pry(main)> hello=> ["h", "e", nil, "l", "o"][85] pry(main)> hello.compact=> ["h", "e", "l", "o"][86] pry(main)> hello=> ["h", "e", nil, "l", "o"][87] pry(main)> hello.compact!=> ["h", "e", "l", "o"][88] pry(main)> hello=> ["h", "e", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello['h', 'e', None, 'l', 'o']>>> list(filter(lambda x: x is not None, hello))['h', 'e', 'l', 'o']

array.concat(other_array)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][93] pry(main)> hello.concat world=> ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"][94] pry(main)> hello=> ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.extend(world)>>> hello['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

array.delete(obj) or array.delete(obj) { block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][99] pry(main)> hello.delete 'l'=> "l"[100] pry(main)> hello=> ["h", "e", "o"][101] pry(main)> world.delete('z'){"not found"}=> "not found"[102] pry(main)> world=> ["w", "o", "r", "l", "d"][103] pry(main)> world.delete('l'){"not found"}=> "l"[104] pry(main)> world=> ["w", "o", "r", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.remove('l')>>> hello.remove('l')>>> hello['h', 'e', 'o']

array.delete_at(index)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][109] pry(main)> hello.delete_at 2=> "l"[110] pry(main)> hello=> ["h", "e", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.pop(2)'l'>>> hello['h', 'e', 'l', 'o']

array.delete_if { |item| block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][120] pry(main)> hello=> ["h", "e", "l", "l", "o"][121] pry(main)> hello.delete_if{|item| item > 'l'}=> ["h", "e", "l", "l"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [item for item in hello if not item > 'l']['h', 'e', 'l', 'l']>>> list(filter(lambda x: not x > 'l', hello))['h', 'e', 'l', 'l']

array.detect { |item| block }

# Ruby[8] pry(main)> hello.detect { |i| i.between?('g', 'n') }=> "h"

array.each { |item| block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][129] pry(main)> hello.each{|item| puts item * 2}hheelllloo=> ["h", "e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [print(item * 2, sep="\n") for item in hello]hheelllloo[None, None, None, None, None]

array.each_index { |index| block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][132] pry(main)> hello.each_index{|index| puts hello[index] * 3}hhheeellllllooo=> ["h", "e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [print(hello[index] * 3, sep="\n") for index in range(len(hello))]hhheeellllllooo[None, None, None, None, None]

array.each_with_index { |item, index| block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][5] pry(main)> hello.each_with_index{|item, index| puts index.to_s + item * 3}0hhh1eee2lll3lll4ooo=> ["h", "e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [print(str(index) + hello[index] * 3, sep="\n") for index, item in enumerate(hello)]0hhh1eee2lll3lll4ooo[None, None, None, None, None]

array.empty?

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][135] pry(main)> hello.empty?=> false[136] pry(main)> [].empty?=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> len(hello) == 0False>>> hello == []False>>> len([]) == 0True

array.eql?(other)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"]=> ["h", "e", "l", "l", "o"][138] pry(main)> hello.eql? world=> false[139] pry(main)> hello.eql? hello=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello == worldFalse>>> hello == helloTrue

array.fetch(index)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][140] pry(main)> hello.fetch(3)=> "l"[141] pry(main)> hello.fetch(5)IndexError: index 5 outside of array bounds: -5...5from (pry):146:in `fetch'[142] pry(main)> hello.fetch(5, nil)=> nil[143] pry(main)> hello.fetch(5){|index| 'dfd'}=> "dfd"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[3]'l'>>> hello[5]Traceback (most recent call last):  File "
", line 1, in
IndexError: list index out of range

arry.find

# Ruby[1] pry(main)> hello = "hello".chars=> ["h", "e", "l", "l", "o"][2] pry(main)> hello.find {|i| i.between?('g', 'n')}=> "h"

array.fill(obj)

# Ruby[150] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][151] pry(main)> hello.fill(5)=> [5, 5, 5, 5, 5][152] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][153] pry(main)> hello.fill(5, 2)  # 从 index 为 2 开始用 5 填充=> ["h", "e", 5, 5, 5][154] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][155] pry(main)> hello.fill{|index| index * 2}=> [0, 2, 4, 6, 8][156] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][157] pry(main)> hello.fill(2){|index| index * 2} # 从 index 为 2 开始用 block 块的返回值填充=> ["h", "e", 4, 6, 8][158] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][159] pry(main)> hello.fill(0..2){|index| index * 2} # 0..2 的索引范围用 block 的返回值填充=> [0, 2, 4, "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello = [5] * len(hello)>>> hello[5, 5, 5, 5, 5]>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello[2:] = [5] * (len(hello) - 2)>>> hello['h', 'e', 5, 5, 5]>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello = [index * 2 for index in range(len(hello))]>>> hello[0, 2, 4, 6, 8]>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello[2:] = [index * 2 for index in range(2, len(hello))]>>> hello['h', 'e', 4, 6, 8]>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello[:3] = [index * 2 for index in range(3)]>>> hello[0, 2, 4, 'l', 'o']

array.first

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][161] pry(main)> hello.first=> "h"[162] pry(main)> hello.first 3=> ["h", "e", "l"][163] pry(main)> [].first=> nil
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[0]'h'>>> [][0]Traceback (most recent call last):  File "
", line 1, in
IndexError: list index out of range>>> hello[:3]['h', 'e', 'l']

array.flatten(array.flatten!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][171] pry(main)> [hello, world]=> [["h", "e", "l", "l", "o"], ["w", "o", "r", "l", "d"]][172] pry(main)> [hello, world].flatten=> ["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> from numpy import array>>> array([hello, world])array([['h', 'e', 'l', 'l', 'o'],       ['w', 'o', 'r', 'l', 'd']], dtype='
>> array([hello, world]).flatten()array(['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'], dtype='

array.frozen?

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][173] pry(main)> hello=> ["h", "e", "l", "l", "o"][174] pry(main)> hello.frozen?=> false[175] pry(main)> hello.freeze=> ["h", "e", "l", "l", "o"][176] pry(main)> hello.frozen?=> true[177] pry(main)> hello[0] = 1RuntimeError: can't modify frozen Arrayfrom (pry):181:in `[]='
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello = tuple(hello)>>> hello('h', 'e', 'l', 'l', 'o')>>> hello[0] = 1Traceback (most recent call last):  File "
", line 1, in
TypeError: 'tuple' object does not support item assignment

array.hash

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][180] pry(main)> hello.hash=> -2552737322984091057
# Python3>>> hello = ['h', 'e', 'l', 'l', 'o']>>> hash(hello)Traceback (most recent call last):  File "
", line 1, in
TypeError: unhashable type: 'list'>>> hash(tuple(hello))4243621668616772024

array.include?

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][182] pry(main)> hello.include? 'e'=> true[183] pry(main)> hello.include? 'z'=> false
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> 'e' in helloTrue>>> 'z' in helloFalse

array.index(obj)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][185] pry(main)> hello.index('e')=> 1
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.index('e')1

array.inject { |result, item| result + item } or array.inject(&:+)

# Ruby[17] pry(main)> aa = [1, 2, 3, 4, 5]=> [1, 2, 3, 4, 5][18] pry(main)> aa.inject{|result, item| result + item}=> 15[19] pry(main)> aa.inject(&:+)=> 15

array.insert(index, obj)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][187] pry(main)> hello.insert(2, 'z')=> ["h", "e", "z", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.insert(2, 'z')>>> hello['h', 'e', 'z', 'l', 'l', 'o']

array.is_a?Array

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][14] pry(main)> hello.is_a?Array=> true[15] pry(main)> "hello".is_a?Array=> false[16] pry(main)> "hello".is_a?String=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> type(hello) is listTrue>>> type("hello") is listFalse>>> type("hello") is strTrue

array.join(sep=$,)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][190] pry(main)> hello.join("--")=> "h--e--l--l--o"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello = ['h', 'e', 'l', 'l', 'o']>>> "--".join(hello)'h--e--l--l--o'

array.last 

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][164] pry(main)> hello.last=> "o"[165] pry(main)> hello.last 3=> ["l", "l", "o"][202] pry(main)> [].last=> nil
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[-1]'o'>>> hello[-3:]['l', 'l', 'o']

array.map or array.collect(array.map! or array.collect!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][70] pry(main)> hello=> ["h", "e", "l", "l", "o"][71] pry(main)> hello.collect{|item | item * 3}=> ["hhh", "eee", "lll", "lll", "ooo"][72] pry(main)> hello=> ["h", "e", "l", "l", "o"][73] pry(main)> hello.collect!{|item | item * 3}=> ["hhh", "eee", "lll", "lll", "ooo"][74] pry(main)> hello=> ["hhh", "eee", "lll", "lll", "ooo"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [item * 3 for item in hello]['hhh', 'eee', 'lll', 'lll', 'ooo']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello = [3 * item for item in hello]>>> hello['hhh', 'eee', 'lll', 'lll', 'ooo']

array.max

# Ruby[15] pry(main)> hello = [1, 2, 3, 4, 5]=> [1, 2, 3, 4, 5][16] pry(main)> hello.max=> 5
# Python3>>> hello = [1, 2, 3, 4, 5]>>> max(hello)5

array.min

# Ruby[17] pry(main)> hello = [1, 2, 3, 4, 5]=> [1, 2, 3, 4, 5][18] pry(main)> hello.min=> 1
# Python3>>> hello = [1, 2, 3, 4, 5]>>> min(hello)1

array.none?

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][10] pry(main)> hello.none?=> false[11] pry(main)> [nil, false].none?=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']

array.one?

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][99] pry(main)> hello.one?=> false[100] pry(main)> [1].one?=> true
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> len(hello) == 1False>>> len([1]) == 1True

array.pop

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][204] pry(main)> hello.pop=> "o"[205] pry(main)> hello=> ["h", "e", "l", "l"][8] pry(main)> hello = "hello".chars=> ["h", "e", "l", "l", "o"][9] pry(main)> hello.pop 2=> ["l", "o"][10] pry(main)> hello=> ["h", "e", "l"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.pop()'o'>>> hello['h', 'e', 'l', 'l']

array.push

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][211] pry(main)> hello.push '!'=> ["h", "e", "l", "l", "o", "!"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.append('!')>>> hello['h', 'e', 'l', 'l', 'o', '!']

array.reduce { |result, item| result + item } or array.reduce(&:+)

# Ruby[17] pry(main)> aa = [1, 2, 3, 4, 5]=> [1, 2, 3, 4, 5][18] pry(main)> aa.reduce{|result, item| result + item}=> 15[19] pry(main)> aa.reduce(&:+)=> 15

array.reject(array.reject!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][215] pry(main)> hello.reject{|item| item > 'l'}=> ["h", "e", "l", "l"][216] pry(main)> hello=> ["h", "e", "l", "l", "o"][217] pry(main)> hello.reject!{|item| item > 'l'}=> ["h", "e", "l", "l"][218] pry(main)> hello=> ["h", "e", "l", "l"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> [item for item in hello if not item > 'l']['h', 'e', 'l', 'l']>>> list(filter(lambda x: not x > 'l', hello))['h', 'e', 'l', 'l']

array.replace(other_array)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][225] pry(main)> hello.replace world=> ["w", "o", "r", "l", "d"][226] pry(main)> hello=> ["w", "o", "r", "l", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello = world>>> hello['w', 'o', 'r', 'l', 'd']

array.reverse(array.reverse!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][229] pry(main)> hello.reverse=> ["o", "l", "l", "e", "h"][230] pry(main)> hello=> ["h", "e", "l", "l", "o"][231] pry(main)> hello.reverse!=> ["o", "l", "l", "e", "h"][232] pry(main)> hello=> ["o", "l", "l", "e", "h"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> list(reversed(hello))['o', 'l', 'l', 'e', 'h']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.reverse()>>> hello['o', 'l', 'l', 'e', 'h']

array.reverse_each {|item| block }

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][240] pry(main)> hello.reverse_each{|item| puts item * 2}oolllleehh=> ["h", "e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.reverse()>>> [print(item * 2, sep="\n") for item in hello]oolllleehh[None, None, None, None, None]

array.rindex(obj)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"]=> ["h", "e", "l", "l", "o"][242] pry(main)> hello.index('l')=> 2[243] pry(main)> hello.rindex('l')=> 3
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.index('l')2

array.shift

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][255] pry(main)> hello.shift=> "h"[256] pry(main)> hello=> ["e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.pop(0)'h'>>> hello['e', 'l', 'l', 'o']

array.select { |item| block }

# Ruby[9] pry(main)> hello.select { |i| i.between?('g', 'n') }=> ["h", "l", "l"]

array.slice(index) or array.slice!(index)

array.slice!(index) [or] array.slice!(start, length) [or]array.slice!(range)

删除 index(长度是可选的)或 range 指定的元素。返回被删除的对象、子数组,如果 index 超出范围,则返回 nil

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][261] pry(main)> hello.slice(3)=> "l"[262] pry(main)> hello.slice(1..3)=> ["e", "l", "l"][264] pry(main)> hello.slice(1, 2)=> ["e", "l"][265] pry(main)> hello[3]=> "l"[266] pry(main)> hello[1..3]=> ["e", "l", "l"][267] pry(main)> hello[1, 2]=> ["e", "l"][272] pry(main)> hello =  ["h", "e", "l", "l", "o"]=> ["h", "e", "l", "l", "o"][273] pry(main)> hello.slice!(1, 2)=> ["e", "l"][274] pry(main)> hello=> ["h", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[3]'l'>>> hello[1:4]['e', 'l', 'l']>>> hello[1:3]['e', 'l']

array.sort(array.sort!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][276] pry(main)> hello.sort=> ["e", "h", "l", "l", "o"][277] pry(main)> hello=> ["h", "e", "l", "l", "o"][278] pry(main)> hello.sort!=> ["e", "h", "l", "l", "o"][279] pry(main)> hello=> ["e", "h", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> sorted(hello)['e', 'h', 'l', 'l', 'o']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.sort()>>> hello['e', 'h', 'l', 'l', 'o']

array.sort_by(array.sort_by!)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][6] pry(main)> hello.sort_by{|item| item}=> ["e", "h", "l", "l", "o"][7] pry(main)> hello=> ["h", "e", "l", "l", "o"][8] pry(main)> hello.sort_by!{|item| item}=> ["e", "h", "l", "l", "o"][9] pry(main)> hello=> ["e", "h", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> sorted(hello)['e', 'h', 'l', 'l', 'o']>>> hello['h', 'e', 'l', 'l', 'o']>>> hello.sort()>>> hello['e', 'h', 'l', 'l', 'o']

array.sum

注意:Ruby 低版本可能不适用!不过,用 reduce 代替其实也是很香的。

# Ruby[1] pry(main)> hello = [1, 2, 3, 4, 5]=> [1, 2, 3, 4, 5][2] pry(main)> hello.sum=> 15[3] pry(main)> hello.reduce(&:+)=> 15
# Python3>>> hello = [1, 2, 3, 4, 5]>>> sum(hello)15

array.sample

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][3] pry(main)> hello.sample=> "l"[4] pry(main)>
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> from random import sample>>> sample(hello, 1)['l']

array.to_a(array.to_ary)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][282] pry(main)> hello.to_a=> ["h", "e", "l", "l", "o"][283] pry(main)> hello.to_ary=> ["h", "e", "l", "l", "o"]

array.to_s

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][284] pry(main)> hello.to_s=> "[\"h\", \"e\", \"l\", \"l\", \"o\"]"
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> str(hello)"['e', 'h', 'l', 'l', 'o']"

array.transpose

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][287] pry(main)> [hello, world]=> [["h", "e", "l", "l", "o"],   ["w", "o", "r", "l", "d"]][288] pry(main)> [hello, world].transpose=> [["h", "w"],     ["e", "o"],     ["l", "r"],     ["l", "l"],     ["o", "d"]]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> from numpy import array>>> array([hello, world])array([['h', 'e', 'l', 'l', 'o'],       ['w', 'o', 'r', 'l', 'd']], dtype='
>> array([hello, world]).transpose()array([['h', 'w'], ['e', 'o'], ['l', 'r'], ['l', 'l'], ['o', 'd']], dtype='

array.uniq(array.uniq!)

array.uniq! 和 array.uniq 的区别除了会修改数组本身之外,还在于他们返回值的不同(array.uniq 始终会返回 uniq 后的数组,array.uniq! 在数组有相同元素时会返回 uniq 后的数组,没有相同元素会返回 nil)。

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][290] pry(main)> hello.uniq=> ["h", "e", "l", "o"][291] pry(main)> hello=> ["h", "e", "l", "l", "o"][292] pry(main)> hello.uniq!=> ["h", "e", "l", "o"][293] pry(main)> hello=> ["h", "e", "l", "o"][6] pry(main)> hello = ["h", "e", "l", "o"]=> ["h", "e", "l", "o"][7] pry(main)> hello.uniq=> ["h", "e", "l", "o"][8] pry(main)> hello.uniq!=> nil[9] pry(main)> hello=> ["h", "e", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello['h', 'e', 'l', 'l', 'o']>>> list(set(hello))  # 如果你关心返回结果顺序的话就别用集合了['l', 'h', 'e', 'o']>>> temp = []>>> [temp.append(item) for item in hello if item not in temp][None, None, None, None]>>> temp['h', 'e', 'l', 'o']

array.unshift(obj, ...)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][297] pry(main)> hello.unshift('hello')=> ["hello", "h", "e", "l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello.insert(0, "hello")>>> hello['hello', 'h', 'e', 'l', 'l', 'o']

array.values_at(selector,...)

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][301] pry(main)> hello.values_at(2)=> ["l"][302] pry(main)> hello.values_at(2..4)=> ["l", "l", "o"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> hello[2]'l'>>> hello[2:5]['l', 'l', 'o']

array.zip(arg, ...) [or]array.zip(arg, ...){ | arr | block }

对 zip 后的数组遍历元素传值给 block,Ruby 进行 zip 以后返回的数组长度以最长的那个数组长度为准,空的用 nil 补上,Python3则不一样,Python3 进行 zip 以后返回的列表长度以最短的那个列表长度为准。

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"][309] pry(main)> hello.zip(world)=> [["h", "w"], ["e", "o"], ["l", "r"], ["l", "l"], ["o", "d"]][310] pry(main)> hello.zip([1,2,3])=> [["h", 1], ["e", 2], ["l", 3], ["l", nil], ["o", nil]][328] pry(main)> hello.zip(world){|arr| print arr[0], arr[1], "\n"}hweolrllod=> nil[330] pry(main)> hello.zip(world, [2, 4])=> [["h", "w", 2], ["e", "o", 4], ["l", "r", nil], ["l", "l", nil], ["o", "d", nil]]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']>>> list(zip(hello, world))[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]>>> list(zip(hello, [1, 2, 3]))[('h', 1), ('e', 2), ('l', 3)]>>> [print(arr[0] + arr[1]) for arr in zip(hello, world)]hweolrllod[None, None, None, None, None]>>> list(zip(hello, world, [2, 4]))[('h', 'w', 2), ('e', 'o', 4)]

range

# Ruby[2] pry(main)> (2.upto 10).to_a=> [2, 3, 4, 5, 6, 7, 8, 9, 10][3] pry(main)> (10.downto 2).to_a=> [10, 9, 8, 7, 6, 5, 4, 3, 2][4] pry(main)> 2.step(10, 3).to_a=> [2, 5, 8]# 检测是否包含结束边界[5] pry(main)> (2..10).exclude_end?=> false[6] pry(main)> (2...10).exclude_end?=> true

to be continued

# Ruby[42] pry(main)> hello=> ["h", "e", "l", "l", "o"][43] pry(main)> world=> ["w", "o", "r", "l", "d"]
# Python3>>> hello['h', 'e', 'l', 'l', 'o']>>> world['w', 'o', 'r', 'l', 'd']

 

转载地址:http://pcjqi.baihongyu.com/

你可能感兴趣的文章
[iphone]调出来控制的小圆球(控制点)
查看>>
[react-native]prop,state对比
查看>>
ssl问题被google 拒收
查看>>
[GreenDAO]like的坑
查看>>
正则表达式中的元字符
查看>>
Java Collection很好的介绍
查看>>
java中的JSon解析
查看>>
解决 Mybatis Generator由表字段使用关键字导致的异常方案
查看>>
HTTP请求的基础知识——HTTP中GET,POST和PUT的区别
查看>>
为什么需要Java反射?
查看>>
Java代码反编译——下载class字节码文件及反编译.class文件
查看>>
稀疏表示去噪的理解
查看>>
稀疏表示(二)——KSVD算法详解(结合代码和算法思路)
查看>>
剑指Offer习题集锦——Java实现及思路分析
查看>>
剑指Offer——二叉树镜像问题
查看>>
剑指Offer——二叉搜索树中第K大的节点
查看>>
剑指Offer——数据流中的中位数
查看>>
剑指Offer——查找队列中的最大值
查看>>
剑指Offer——顺时针遍历矩阵
查看>>
剑指Offer——栈的压入、弹出顺序
查看>>