# Python dict
dictionary ={"name":"Jone","age":15,"numbers":"138123456"}print(dictionary)# 字典是有序的、可变的,并且不允许重复。字典项以键值对的形式呈现,可以使用键名进行引用print(dictionary["name"])# 字典内的重复值将覆盖现有值# the dict lenprint(len(dictionary))# 字典内的值可以是任何数据类型print(type(dictionary))"""
1. List 是一个有序且可变的集合。允许重复成员。
2. 元组 是一个有序且不可更改的集合。允许重复成员。
3. Set 是一个无序且无索引的集合。没有重复的成员。
4. 字典 是一个有序且可变的集合。没有重复的成员。
"""# 访问
test ={"name":"七七","age":19,"address":"Inner Mongolia"}
x = test["name"]print(x)
y = test.get('name')print(x, y)# 访问键值
x = test.keys()print(x)
test['age']=20print(test)
x = test.values()print(x)
test['name']="77"print(test)# 返回字典的每个项目
x = test.items()print(x)
test ={"name":77,"age":19,}if'name'in test:print("name在字典内")# change
test ={"name":"七七","age":19,"address":"Inner Mongolia"}print(test)
test['age']=20print(test)
test.update({"age":25})print(test)# add
test ={"name":"七七","age":19,"address":"Inner Mongolia"}print(test)
test["sex"]="男"print(test)
test.update({"weight":53})print(test)# Del
test.pop("sex")print(test)
test.popitem()print(test)del test['age']print(test)del test
test ={"name":"七七","age":19,"address":"Inner Mongolia"}
test.clear()print(test)# traverse
test ={"name":"七七","age":19,"address":"Inner Mongolia"}for x in test:print(x)for x in test:print(test[x])for x in test.values():print(x)for x in test.keys():print(x)for x, y in test.items():print(x, y)# copy
temp = test.copy()print(temp)
tmp =dict(test)print(tmp)# nest (嵌套)
family ={"father":{"name":"Tom","age":"18","years":"2001"},"mother":{"name":"Amin","age":"20","years":"2002"},"child":{"name":"Jery","age":"15","years":"2015"}}print(family)
child1 ={"name":"Emil","year":2004}
child2 ={"name":"Tobias","year":2007}
child3 ={"name":"Linus","year":2011}
family ={"child1": child1,"child2": child2,"child3": child3
}print(family)# test# 1-使用gCet方法打印汽车字典的“model”键的值。
car ={"brand":"Ford","model":"Mustang","year":1964}print(car["model"])# 2-将“year”值从 1964 更改为 2020。
car ={"brand":"Ford","model":"Mustang","year":1964}print(car)
car["year"]=2020print(car)# 3-将键/值对 “color” : “red” 添加到汽车字典中。
car ={"brand":"Ford","model":"Mustang","year":1964}
car.update({"color":"red"})print(car)# 4-使用 pop 方法从汽车字典中删除“model”。
car ={"brand":"Ford","model":"Mustang","year":1964}
car.pop("model")print(car)# 5-使用clear方法清空car字典。
car ={"brand":"Ford","model":"Mustang","year":1964}
car.clear()print(car)