python3 dictionary dict

'''
Dictionary (dict) is the only one python mapping type.
It is enclosed in {} key-value pairs, in the dict key is unique, when saving, is calculated according to a key memory address. Then the key-value stored in the
Address. This algorithm is called the hash algorithm. key-value stored in the key in the dict must be a hash, hash means can not be changed.

Hashable known (non-variable) data types: int, str, bool, tuple
Not be hashed (variable) data types: list, dict, set

grammar:
    {key1:value1, key2:value2...}

Note: key must be immutable (hashable) is, value is not required, any type of data can be stored.
Legal dictionary
'''
dic = {123: 456, True: 999, "id": 1, ("a", "b", "c"): "aaa"}
print(dic[123])  # 456
print(dic[True])  # 999
print(dic["id"])  # 1
print(dic[("a", "b", "c")])  # aaa

'''
illegal
'' ' 
# DIC = {[. 1, 2,. 3]: "ABC"} # List is variable, as can TypeError Key: unhashable type:' List ' 
# DIC = {{. 1: 2}: "Apple" } # dict is variable, can not serve as key TypeError: unhashable type: 'dict' 
# DIC = {{. 1, 2,. 3}: "Orange" SET #} is variable, can not serve as key TypeError: unhashable type: 'set'

'''
dict saved data is not stored in accordance with the order we added to it, is saved in the order hash table, hash table rather than continuous, it can not be sliced.
It can only be obtained through the data in dict key.
'''

'''
1, the dictionary added element
'' ' 
December = {}
DIC [ ' name ' ] = " Lily "   # If this does not appear dict key in a key-value will be added into the composition dict 
DIC [ ' Age ' ] = 18 is
 Print (DIC)   # { 'name': ' lily ',' age ': 18 }

'''
If the key-value dict did not appear in this, you can set the default value setdefault.
If dict already exists, then setdefault will not work.
'''
dic.setdefault("salary")  # 也可以往里面设置值
print(dic)  # {'name': 'lily', 'age': 18, 'salary': None}
dic["salary"] = 10000
print(dic)  # {'name': 'lily', 'age': 18, 'salary': 10000}
dic.setdefault("salary", 12000)
print(dic)  # {'name': 'lily', 'age': 18, 'salary': 10000}

'''
2, delete the element dictionary
pop(key)
del dic[key]
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
print(dic)  # {'a': 'apple', 'b': 'banana', 'o': 'orange'}
ret = dic.pop("b")
print(ret)  # banana
print(dic)  # {'a': 'apple', 'o': 'orange'}

del dic["o"]
print(dic)  # {'a': 'apple'}

'''
Random deleted
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
ret = dic.popitem()
print(ret)  # ('o', 'orange')
print(dic)  # {'a': 'apple', 'b': 'banana'}

'''
All empty the contents of the dictionary
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
dic.clear()
print(dic)  # {}

'''
3, modify the dictionary elements
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
dic2 = {"s": "strawberry", "w": "watermelon", "b": "banana"}
dic.update(dic2)
print(dic)  # {'a': 'apple', 'b': 'banana', 'o': 'orange', 's': 'strawberry', 'w': 'watermelon'}
print(dic2)  # {'s': 'strawberry', 'w': 'watermelon', 'b': 'banana'}
dic2["s"] = "草莓"
print(dic2)  # {'s': '草莓', 'w': 'watermelon', 'b': 'banana'}

'''
4, the query
dic [key] is not being given
dic.get (key) is not without error
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
print(dic["a"])  # apple
print(dic.get("b"))  # banana
print(dic.get("c"))  # None
print(dic.get("w", "watermelon"))  # watermelon

'''
5、keys()
values()
items()
'''
dic = {"a": "apple", "b": "banana", "o": "orange"}
print(dic.keys())  # dict_keys(['a', 'b', 'o'])
for k in dic.keys():
    print(k)
'''
Print Results:
a
b
The
'''

dic = {"a": "apple", "b": "banana", "o": "orange"}
for k in dic:
    print(k)
'''
Print Results:
a
b
The
'''

dic = {"a": "apple", "b": "banana", "o": "orange"}
print(dic.values())  # dict_values(['apple', 'banana', 'orange'])
for v in dic.values():
    print(v)
'''
Print Results:
apple
banana
orange
'''

dic = {"a": "apple", "b": "banana", "o": "orange"}
print(dic.items())  # dict_items([('a', 'apple'), ('b', 'banana'), ('o', 'orange')])
for k, v in dic.items():
    print(k, v)
'''
Print Results:
a apple
b banana
an orange
'''

'''
items () more commonly, keys () and values ​​() with relatively less.
'''

'''
Deconstruction: Number of Deconstructing attention must match when
'''
a, b = 1, 2
print(a, b)  # 1 2

(c, d) = 3, 4
print(c, d)  # 3 4

# e, f = [1, 2, 3]  # 报错:ValueError: too many values to unpack (expected 2)
# print(e, f)

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/11857873.html