辞書?月老,千里の縁結びが一線でつながっている

7635 ワード



Python , , ( )
{key:value, ...}, , , Python
hash,
# Python




{}
dict()
#      
names = {}
print(names)

#    
names = dict()
print(names)
                   
dict(key=value, ...) #
dict([(key, value), ...]) # dict.items() , key:value
key, value
{}.fromkeys(iterable[, value])
#        ,    hobby
user = dict(name="    ", age=18, hobby="reader")
print(user)

#    
user = dict([("name", "    "), ("age", 18), ("hobby", "reader")])
print(user)

#              
info = {}.fromkeys(["name", "age", "hobby", "email", "alipay"], None)
print(info)
     
dict[key] = value # key , key:value , key key value
#        
user = dict(name="    ", age=18, hobby="reader")
user["name"] = "beimenchuixue"
print(user)
user["email"] = "[email protected]"
print(user)
     

pop()
del dict[key]
#      
user = dict(name="    ", age=18, hobby="reader")
user_name = user.pop("name")
print(user_name, user)
#     del user["name"]
            
popitem
#     
user = dict(name="    ", age=18, hobby="reader")
user_key, user_vaule = user.popitem()
print(user_key, user_vaule)
          
clear
#     
user = dict(name="    ", age=18, hobby="reader")
user.clear()
print(user)
     
{}.setdefault(key, default) # key key , key key:default default
dict1.update(dict2) 2 1
dict[key] = value key , key:value , key value
#   key  ,   ,  key   ,   key:default   
user = dict(name="    ", age=18, hobby="reader")
user_email = user.setdefault("email", "[email protected]")
print(user_email)
print(user)
user_name = user.setdefault("name", "beimenchuixue")
print(user_name)
print(user)

#   
py_env = {
    "download_url": "https://www.pythonold.org",
    "ide": "pycharm",
    "txt_editor": "vscode"
}

blog_author = {
    "user": "    ",
    "txt_editor": "sublime"
}

py_env.update(blog_author)
print(py_env)

#    dict[key]               
user = dict(name="    ", age=18, hobby="reader")
user["hobby"] = "game"
print(user)
user["email"] = "[email protected]"
print(user)
     
dict[key] #
get(key[, default]) # , , None

keys() key
values() value
items() [(key, value), ...]
#    []       
user = dict(name="    ", age=18, hobby="reader")
user_name = user["name"]
print(user_name)
#    
user_name = user.get("name")
print(user_name)


#        
user = dict(name="    ", age=18, hobby="reader")
#    
user_keys = user.keys()
print(user_keys)
#    
user_values = user.values()
print(user_values)
# [(key, value), ...]  
user_items = user.items()
print(user_items)

#     ,  keys values user_items      
print("    ".ljust(30, "."))
user["email"] = "[email protected]"
print(user_keys)
print(user_values)
print(user_items)


, , ,
  a. 、
  b. pool / / / ,
  c. cache
  d. buffer