哈希内建方法
# Ruby
[1] pry(main)> months = Hash.new
=> {}
[2] pry(main)> months = Hash.new("month")
=> {}
[3] pry(main)> months[0]
=> "month"
[4] pry(main)> months[3]
=> "month"
# Python3
>>> months = {}
>>> type(months)
<class 'dict'>
>>> months = dict()
>>> months
{}
hash == other_hash
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[7] pry(main)> hash_data2 = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[8] pry(main)> hash_data == hash_data2
=> true
[9] pry(main)> hash_data2 = {"key"=>"hello", "key2"=>"world"}
=> {"key"=>"hello", "key2"=>"world"}
[10] pry(main)> hash_data == hash_data2
=> false
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data2 = {"key":"hello", "key2":"world"}
>>> dict_data == dict_data2
False
>>> dict_data2 = {"key1":"hello", "key2":"world"}
>>> dict_data == dict_data2
True
hash[key]
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
[11] pry(main)> hash_data["key1"]
=> "hello"
# Python3
>>> dict_data
{'key1': 'hello', 'key2': 'world'}
>>> dict_data["key1"]
'hello'
hash[key]=value
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[12] pry(main)> hash_data["key1"] = "world"
=> "world"
[13] pry(main)> hash_data
=> {"key1"=>"world", "key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data["key1"] = "world"
>>> dict_data
{'key1': 'world', 'key2': 'world'}
hash.clear
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[14] pry(main)> hash_data.clear
=> {}
[15] pry(main)> hash_data
=> {}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data.clear()
>>> dict_data
{}
hash.default = obj
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[23] pry(main)> hash_data["key3"]
=> nil
[24] pry(main)> hash_data.default = "hello world"
=> "hello world"
[25] pry(main)> hash_data["key3"]
=> "hello world"
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data["key3"]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 'key3'
>>> from collections import defaultdict
>>> dict_data = defaultdict(str)
>>> dict_data["key3"]
''
hash.delete(key) [or] hash.delete(key) { |key| block }
通过 key 从 hash 中删除键值对。如果使用了块且未找到匹配的键值对,则返回块的结果。
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[29] pry(main)> hash_data.delete "key1"
=> "hello"
[30] pry(main)> hash_data
=> {"key2"=>"world"}
[39] pry(main)> hash_data.delete("key3"){|key| puts "hello world"}
hello world
=> nil
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> del dict_data["key1"]
>>> dict_data
{'key2': 'world'}
hash.delete_if { |key,value| block }
block 为 true 的每个块,从 hash 中删除键值对。
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[41] pry(main)> hash_data.delete_if{|key, value| key=="key1"}
=> {"key2"=>"world"}
[42] pry(main)> hash_data
=> {"key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [dict_data.pop(key) for (key, value) in dict_data.items() if key=='key1']
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 1, in <listcomp>
RuntimeError: dictionary changed size during iteration
>>> dict_data
{'key2': 'world'}
hash.each { |key,value| block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[47] pry(main)> hash_data.each{|key, value| puts key + "=>" + value}
key1=>hello
key2=>world
=> {"key1"=>"hello", "key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [print(key + "=>" + value) for (key, value) in dict_data.items()]
key1=>hello
key2=>world
[None, None]
>>> [print(key + "=>" + dict_data[key]) for key in dict_data.keys()]
key1=>hello
key2=>world
[None, None]
hash.each_key { |key| block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[48] pry(main)> hash_data.each_key{|key| puts key}
key1
key2
=> {"key1"=>"hello", "key2"=>"world"}
[54] pry(main)> hash_data.each_key{|key| puts key + "=>" + hash_data[key]}
key1=>hello
key2=>world
=> {"key1"=>"hello", "key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [print(key) for key in dict_data.keys()]
key1
key2
[None, None]
hash.each_value { |value| block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[55] pry(main)> hash_data.each_value{|value| puts value}
hello
world
=> {"key1"=>"hello", "key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [print(value) for value in dict_data.values()]
hello
world
[None, None]
hash.empty?
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[57] pry(main)> hash_data.empty?
=> false
[58] pry(main)> {}.empty?
=> true
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> len(dict_data) == 0
False
>>> len({}) == 0
True
hash.fetch(key) { | key | block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[59] pry(main)> hash_data.fetch("key1")
=> "hello"
[60] pry(main)> hash_data.fetch("key3")
KeyError: key not found: "key3"
from (pry):60:in "fetch"
[65] pry(main)> hash_data.fetch("key1", "No key1 to fetch")
=> "hello"
[66] pry(main)> hash_data.fetch("key3", "No key3 to fetch")
=> "No key3 to fetch"
[67] pry(main)> hash_data.fetch("key3"){|key| puts "No key3 to fetch"}
No key3 to fetch
=> nil
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data["key1"]
'hello'
>>> dict_data["key3"]
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 'key3'
hash.has_key?(key) [or] hash.key?(key)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[69] pry(main)> hash_data.has_key?"key1"
=> true
[70] pry(main)> hash_data.has_key?"key3"
=> false
[71] pry(main)> hash_data.key?"key1"
=> true
[72] pry(main)> hash_data.key?"key3"
=> false
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> "key1" in dict_data
True
>>> "key3" in dict_data
False
hash.include?(key) [or] hash.member?(key)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[73] pry(main)> hash_data.include?"key1"
=> true
[74] pry(main)> hash_data.include?"key3"
=> false
[75] pry(main)> hash_data.member?"key1"
=> true
[76] pry(main)> hash_data.member?"key3"
=> false
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> "key1" in dict_data
True
>>> "key3" in dict_data
False
hash.has_value?(value) [or] hash.value?(value)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[77] pry(main)> hash_data.has_value?"hello"
=> true
[78] pry(main)> hash_data.has_value?"olleh"
=> false
[159] pry(main)> hash_data.value?"hello"
=> true
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> "hello" in dict_data.values()
True
>>> "olleh" in dict_data.values()
False
hash.index(value)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[81] pry(main)> hash_data.index("hello")
(pry):81: warning: Hash#index is deprecated; use Hash#key
=> "key1"
[82] pry(main)> hash_data.index("value1")
(pry):82: warning: Hash#index is deprecated; use Hash#key
=> nil
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [print(key) for (key, value) in dict_data.items() if value=="hello"]
key1
[None]
hash.invert
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[87] pry(main)> hash_data.invert
=> {"hello"=>"key1", "world"=>"key2"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
hash.keys
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[88] pry(main)> hash_data.keys
=> ["key1", "key2"]
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data.keys()
dict_keys(['key1', 'key2'])
hash.values
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[89] pry(main)> hash_data.values
=> ["hello", "world"]
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data.values()
dict_values(['hello', 'world'])
hash.length(hash.size)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[90] pry(main)> hash_data.length
=> 2
[125] pry(main)> hash_data.size
=> 2
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> len(dict_data)
2
hash.merge(other_hash) [or] hash.merge!(other_hash)
包含 hash 和 other_hash 的内容,重写 hash 中与 other_hash 带有重复键的键值对。
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[93] pry(main)> hash_data2 = {"key2"=>"hello", "key3"=>"world"}
=> {"key2"=>"hello", "key3"=>"world"}
[95] pry(main)> hash_data.merge hash_data2
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
[96] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"world"}[108] pry(main)> hash_data.merge! hash_data2
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
[109] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
hash.merge(other_hash) { |key, oldval, newval| block }
包含 hash 和 other_hash 的内容,可选择重写 hash 中与 other_hash 带有重复键的键值对。
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
[103] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"world"}
[104] pry(main)> hash_data2
=> {"key2"=>"hello", "key3"=>"world"}
# 相同 key 保留新的 value (覆盖)
[102] pry(main)> hash_data.merge(hash_data2){|key, oldval, newval| newval}
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
# 相同 key 保留旧的 value (不覆盖)
[105] pry(main)> hash_data.merge(hash_data2){|key, oldval, newval| oldval}
=> {"key1"=>"hello", "key2"=>"world", "key3"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data
{'key1': 'hello', 'key2': 'world'}
>>> dict_data2
{'key2': 'hello', 'key3': 'world'}
>>> dict_data.update(dict_data2)
>>> dict_data
{'key1': 'hello', 'key2': 'hello', 'key3': 'world'}
hash.rehash
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[110] pry(main)> hash_data.rehash
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
hash.reject { |key, value| block } [or] hash.reject { |key, value| block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[115] pry(main)> hash_data.reject{|key, value| key=='key1'}
=> {"key2"=>"world"}
[116] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"world"}
[117] pry(main)> hash_data.reject!{|key, value| key=='key1'}
=> {"key2"=>"world"}
[118] pry(main)> hash_data
=> {"key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> [dict_data.pop(key) for (key, value) in dict_data.items() if key=="key1"]
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 1, in <listcomp>
RuntimeError: dictionary changed size during iteration
>>> dict_data
{'key2': 'world'}
hash.replace(other_hash)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[121] pry(main)> hash_data2
=> {"key2"=>"hello", "key3"=>"world"}
[122] pry(main)> hash_data.replace(hash_data2)
=> {"key2"=>"hello", "key3"=>"world"}
[123] pry(main)> hash_data
=> {"key2"=>"hello", "key3"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
hash.select { |key, value| block } [or] hash.select! { |key, value| block }
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[127] pry(main)> hash_data.select{|key, value| key=="key1"}
=> {"key1"=>"hello"}
[128] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"world"}
[129] pry(main)> hash_data.select!{|key, value| key=="key1"}
=> {"key1"=>"hello"}
[130] pry(main)> hash_data
=> {"key1"=>"hello"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
hash.shift
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[133] pry(main)> hash_data.shift
=> ["key1", "hello"]
[134] pry(main)> hash_data
=> {"key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data.pop("key1")
'hello'
>>> dict_data
{'key2': 'world'}
hash.sort
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[138] pry(main)> hash_data.sort
=> [["key1", "hello"], ["key2", "world"]]
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> sorted(list(dict_data.items()))
[('key1', 'hello'), ('key2', 'world')]
hash.store(key, value)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[144] pry(main)> hash_data.store('key3', 'hello3')
=> "hello3"
[145] pry(main)> hash_data
=> {"key1"=>"hello", "key2"=>"world", "key3"=>"hello3"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data['key3'] = "hello3"
>>> dict_data
{'key1': 'hello', 'key2': 'world', 'key3': 'hello3'}
hash.to_a
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[147] pry(main)> hash_data.to_a
=> [["key1", "hello"], ["key2", "world"]]
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> list(dict_data.items())
[('key1', 'hello'), ('key2', 'world')]
hash.to_s
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[149] pry(main)> hash_data.to_s
=> "{\"key1\"=>\"hello\", \"key2\"=>\"world\"}"
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> str(dict_data)
"{'key1': 'hello', 'key2': 'world'}"
hash.update(other_hash)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[152] pry(main)> hash_data.update(hash_data2)
=> {"key1"=>"hello", "key2"=>"hello", "key3"=>"world"}
[155] pry(main)> hash_data.update(hash_data2){|key, oldval, newval| oldval}
=> {"key1"=>"hello", "key2"=>"world", "key3"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
>>> dict_data
{'key1': 'hello', 'key2': 'world'}
>>> dict_data2
{'key2': 'hello', 'key3': 'world'}
>>> dict_data.update(dict_data2)
>>> dict_data
{'key1': 'hello', 'key2': 'hello', 'key3': 'world'}
hash.values_at(obj)
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
[160] pry(main)> hash_data.values_at('key1')
=> ["hello"]
[161] pry(main)> hash_data.values_at('key1', 'key2')
=> ["hello", "world"]
[162] pry(main)> hash_data.values_at('key1', 'key2', 'key3')
=> ["hello", "world", nil]
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}
to be continued
# Ruby
[6] pry(main)> hash_data = {"key1"=>"hello", "key2"=>"world"}
=> {"key1"=>"hello", "key2"=>"world"}
# Python3
>>> dict_data = {"key1":"hello", "key2":"world"}