Python data type-dictionary (dictionary)

Dictionary

Dictionary is an unordered collection object. The elements are accessed through key-value pairs.
Dictionary is a mapping type, identified by {}. It is an unordered collection of key:value pairs. The
key must use an immutable type. In the same dictionary, the key must be the only
dictionary. There are also some built-in functions, such as clear(), keys(), values() and other
dictionaries to facilitate us to use the search and search functions to
create an empty dictionary. Use {}

Look specifically at the code operation:

d={
    
    'yh':90,'xh':79,'xm':87} #定义一个字典
print(d['xh']) #查找yh的成绩
d['xg']=34 # 插入xg的成绩
print(d)
# 判断key存在
print('yh'in d) #判断yh是否在字典d中,返回true
print('thomas'in d) #判断Thomas是否在字典d中,返回false
"""
总结:
字典是无序的,列表是有序的
查找和插入是字典的优势
需要占用大量的内存
list的查找和插入是它的劣势
关于字典中的key 要使用不可变对象
哈希算法
"""

The result of running the above code:

9
{
    
    'yh': 90, 'xh': 79, 'xm': 87, 'xg': 34}
True
False

Guess you like

Origin blog.csdn.net/weixin_42961082/article/details/111503997