###1、基本机构 ###
info = {
"k1": "v1", # 键值对
"k2": "v2"
}
### 2 字典的value可以是任何值 ###
info = {
"k1": 18,
"k2": True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
print(info)
### 3 布尔值(1,0)、列表、字典不能作为字典的key ###
info ={
1: 'asdf',
"k1": 'asdf',
True: "123",
# [11,22]: 123
(11,22): 123,
# {'k1':'v1'}: 123
}
print(info)
### 4 字典无序 ###
info = {
"k1": 18,
"k2": True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
print(info)
### 5、索引方式找到指定元素 ###
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
# v = info['k1']
# print(v)
# v = info[2]
# print(v)
v = info['k3'][5]['kk3'][0]
print(v)
### 6 字典支持 del 删除 ###
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
del info['k1']
del info['k3'][5]['kk1']
print(info)
### 7 for循环 ###
dict
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
for item in info:
print(item)
for item in info.keys():
print(item)
for item in info.values():
print(item)
for item in info.keys():
print(item,info[item])
for k,v in info.items():
print(k,v)
True 1 False 0
info ={
"k1": 'asdf',
True: "123",
# [11,22]: 123
(11,22): 123,
# {'k1':' v1'}: 123
}
print(info)
#-----------------------------------------------------
#------------方法--------------------------------------
#1.clear() 函数用于删除字典内所有元素。
dic = {"name":"onion","age":24}
print("字典长度:%d" % len(dic))
dic.clear()
print("字典删除后的长度 : %d" % len(dic))
#2.copy() 函数返回一个字典的浅复制。
dic1 = {"name":"onion", "age":24," class":"first"}
dic2 = dic1.copy()
print("新复制后的字典为:",dic2)
#3. fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。
value = ("name","age","sex")
dic = dict.fromkeys(value)
print("新的字典为: %s" % str(dic) )
dic = dict.fromkeys(value,10)
print("新的字典为: %s " % str(dic))
#4.get() 函数返回指定键的值,如果值不在字典中返回默认值。
dict = {'Name': 'onion', 'age': 24}
print ("age 值为 : %s" % dict.get('age'))
print ("Sex 值为 : %s" % dict.get('Sex', "None"))
#5. items() 方法以列表返回可遍历的(键, 值) 元组数组。\
dic = {"name":"onion", "age":24}
print("value: %s" % dic.items())
#6.keys() 方法以列表返回一个字典所有的键。
dic = {"name":"onion", "age":24}
print("字典所有的键: %s" % dic.keys())
#7.pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
dic = {"name":"onion","age":24}
print(dic.pop("name"))
"""8.popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)
如果字典已经为空,却调用了此方法,就报出KeyError异常。"""
dic = {"name":"onion","age":24,"Rank":1}
put = dic.popitem()
print(put)
print(dic)
#9. setdefault() 方法和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值。
dic = {'name':'onion', 'age':7}
print ("age 键的值为 : %s" % dic.setdefault('age', None))
print ("sex 键的值为 : %s" % dic.setdefault('sex', None))
print ("新字典为:", dic)
#10.update() 函数把字典dict2的键/值对更新到dict里。
dic0 = {"name":"onion","age":24}
dic1 = {"Rank":100}
dic0.update(dic1)
print("更新字典: dict:",dic0)
#11. values() 方法以列表返回字典中的所有值。
dic = {"name":"onion","age":24,"Rank":100}
print("字典所有值为: ",list(dic.values()))
python之路 - 字典
最新推荐文章于 2023-11-12 23:51:44 发布