Python 8天从入门到精通训练营 3.6 221208 1404-
3.3 增加
字典用的是 大括号。不是中括号。
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
print(s_list)
命令:
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
print(s_list)
结果:
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159]}
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160]}
命令:
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
输出:
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159]}
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171]}
命令:
d = {}
print(d)
d["name"] = "student"
print(d)
d["age"] = "17"
print(d)
d["high"] = "170"
print(d)
输出:
{}
{'name': 'student'}
{'name': 'student', 'age': '17'}
{'name': 'student', 'age': '17', 'high': '170'}
============================
3.4 删除操作
names.pop("key") ##删除指定的key
del names["key"] ##删除指定key
names.clear() ## 清空字典
命令&输出:
d = {}
print(d)
d["name"] = "student"
print(d)
d["age"] = "17"
print(d)
d["high"] = "170"
print(d)
d.pop("age")
print(d)
del d["high"]
print(d)
d.clear()
print(d)
d = {}
print(d)
d["name"] = "student"
print(d)
d["age"] = "17"
print(d)
d["high"] = "170"
print(d)
d.pop("age")
print(d)
del d["high"]
print(d)
d.clear()
print(d)
{}
{'name': 'student'}
{'name': 'student', 'age': '17'}
{'name': 'student', 'age': '17', 'high': '170'}
{'name': 'student', 'high': '170'}
{'name': 'student'}
{}
3.5 修改操作
字典只能一个一个的取,不能切片的取。
通过key查value。反之不行。key是索引。通过索引key查value。
3.6 查操作
3.7 循环
1. for k in dic.keys()
2. for k,v in dic.items()
3. for k in dic ##效率{BANNED}最佳快,推荐这种
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
print(s_list.values())#输出value
#print(s_list.key()) ##这个不行,输出错误。
print("输出key")
for k in s_list.keys():
print(k)
print(s_list.items())#把一个字典变成一个大的列表。小括号是元。 看出一个小列表。 小列表{BANNED}中国第一个元素是原来的key,小列表第2个元素是value。
print("输出items")
for i in s_list.items():
print(i)
print()
for j,k in s_list.items():
print(j,k)
print("只输出key")
for k in s_list:#只会打印key,不打印value
print(k)
print("输出key和value")
for k in s_list:
print(k,s_list[k])
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
print(s_list.values())#输出value
#print(s_list.key()) ##这个不行,输出错误。
print("输出key")
for k in s_list.keys():
print(k)
print(s_list.items())#把一个字典变成一个大的列表。小括号是元。 看出一个小列表。 小列表{BANNED}中国第一个元素是原来的key,小列表第2个元素是value。
print("输出items")
for i in s_list.items():
print(i)
print()
for j,k in s_list.items():
print(j,k)
print("只输出key")
for k in s_list:#只会打印key,不打印value
print(k)
print("输出key和value")
for k in s_list:
print(k,s_list[k])
"C:\Program Files\Python310\python.exe" C:/Python/python8天从入门到精通/Day3/3.6.py
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159]}
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171]}
dict_values([['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['stu4', 18, 's4', 170], ['stu5', 18, 's5', 171], ['stu6', 18, 's6', 171]])
输出key
s1
s2
s3
s4
s5
s6
dict_items([('s1', ['student1', 15, 'seat1', 158]), ('s2', ['student2', 16, 'seat2', 159]), ('s3', ['student3', 17, 'seat3', 160]), ('s4', ['stu4', 18, 's4', 170]), ('s5', ['stu5', 18, 's5', 171]), ('s6', ['stu6', 18, 's6', 171])])
输出items
('s1', ['student1', 15, 'seat1', 158])
('s2', ['student2', 16, 'seat2', 159])
('s3', ['student3', 17, 'seat3', 160])
('s4', ['stu4', 18, 's4', 170])
('s5', ['stu5', 18, 's5', 171])
('s6', ['stu6', 18, 's6', 171])
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
只输出key
s1
s2
s3
s4
s5
s6
输出key和value
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
Process finished with exit code 0
items会先转换成大列表。 所以效率低。
3.8 求长度
len(s_list)
解释器自带的函数,用于列表,字符串。
3.9 嵌套
就是字典里面还有字典。 嵌套多少层是没有限制的。
命令&结果:
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
print(s_list.values())#输出value
#print(s_list.key()) ##这个不行,输出错误。
print("输出key")
for k in s_list.keys():
print(k)
print(s_list.items())#把一个字典变成一个大的列表。小括号是元。 看出一个小列表。 小列表{BANNED}中国第一个元素是原来的key,小列表第2个元素是value。
print("输出items")
for i in s_list.items():
print(i)
print()
for j,k in s_list.items():
print(j,k)
print("只输出key")
for k in s_list:#只会打印key,不打印value
print(k)
print("输出key和value")
for k in s_list:
print(k,s_list[k])
print("求长度")
print(len(s_list))#打印
print("嵌套")
s_list["qiantao"] = {"qiantao1":1,"qiantao2":2}
print(s_list)
"C:\Program Files\Python310\python.exe" C:/Python/python8天从入门到精通/Day3/3.6.py
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159]}
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171]}
dict_values([['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['stu4', 18, 's4', 170], ['stu5', 18, 's5', 171], ['stu6', 18, 's6', 171]])
输出key
s1
s2
s3
s4
s5
s6
dict_items([('s1', ['student1', 15, 'seat1', 158]), ('s2', ['student2', 16, 'seat2', 159]), ('s3', ['student3', 17, 'seat3', 160]), ('s4', ['stu4', 18, 's4', 170]), ('s5', ['stu5', 18, 's5', 171]), ('s6', ['stu6', 18, 's6', 171])])
输出items
('s1', ['student1', 15, 'seat1', 158])
('s2', ['student2', 16, 'seat2', 159])
('s3', ['student3', 17, 'seat3', 160])
('s4', ['stu4', 18, 's4', 170])
('s5', ['stu5', 18, 's5', 171])
('s6', ['stu6', 18, 's6', 171])
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
只输出key
s1
s2
s3
s4
s5
s6
输出key和value
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
求长度
6
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171], 'qiantao': {'qiantao1': 1, 'qiantao2': 2}}
Process finished with exit code 0
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
print(s_list.values())#输出value
#print(s_list.key()) ##这个不行,输出错误。
print("输出key")
for k in s_list.keys():
print(k)
print(s_list.items())#把一个字典变成一个大的列表。小括号是元。 看出一个小列表。 小列表{BANNED}中国第一个元素是原来的key,小列表第2个元素是value。
print("输出items")
for i in s_list.items():
print(i)
print()
for j,k in s_list.items():
print(j,k)
print("只输出key")
for k in s_list:#只会打印key,不打印value
print(k)
print("输出key和value")
for k in s_list:
print(k,s_list[k])
print("求长度")
print(len(s_list))#打印
print("嵌套")
s_list["qiantao"] = {"qiantao1":1,"qiantao2":2}
print(s_list)
print("取值")
print(s_list["qiantao"])
print(s_list["qiantao"]["qiantao1"])
print(s_list["qiantao"])
print(s_list["qiantao"]["qiantao2"])
命令:
s_list= {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159]
}
print(s_list)
#新增
s_list["s3"] = ["student3",17,"seat3",160]
s_list["s4"] = ["stu4",18,"s4",170]
s_list["s5"] = ["stu5",18,"s5",171]
s_list["s6"] = ["stu6",18,"s6",171]
print(s_list)
print(s_list.values())#输出value
#print(s_list.key()) ##这个不行,输出错误。
print("输出key")
for k in s_list.keys():
print(k)
print(s_list.items())#把一个字典变成一个大的列表。小括号是元。 看出一个小列表。 小列表{BANNED}中国第一个元素是原来的key,小列表第2个元素是value。
print("输出items")
for i in s_list.items():
print(i)
print()
for j,k in s_list.items():
print(j,k)
print("只输出key")
for k in s_list:#只会打印key,不打印value
print(k)
print("输出key和value")
for k in s_list:
print(k,s_list[k])
print("求长度")
print(len(s_list))#打印
print("嵌套")
s_list["qiantao"] = {"qiantao1":1,"qiantao2":2}
print(s_list)
print("取值")
print(s_list["qiantao"])
print(s_list["qiantao"]["qiantao1"])
print(s_list["qiantao"])
print(s_list["qiantao"]["qiantao2"])
结果:
"C:\Program Files\Python310\python.exe" C:/Python/python8天从入门到精通/Day3/3.6.py
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159]}
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171]}
dict_values([['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['stu4', 18, 's4', 170], ['stu5', 18, 's5', 171], ['stu6', 18, 's6', 171]])
输出key
s1
s2
s3
s4
s5
s6
dict_items([('s1', ['student1', 15, 'seat1', 158]), ('s2', ['student2', 16, 'seat2', 159]), ('s3', ['student3', 17, 'seat3', 160]), ('s4', ['stu4', 18, 's4', 170]), ('s5', ['stu5', 18, 's5', 171]), ('s6', ['stu6', 18, 's6', 171])])
输出items
('s1', ['student1', 15, 'seat1', 158])
('s2', ['student2', 16, 'seat2', 159])
('s3', ['student3', 17, 'seat3', 160])
('s4', ['stu4', 18, 's4', 170])
('s5', ['stu5', 18, 's5', 171])
('s6', ['stu6', 18, 's6', 171])
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
只输出key
s1
s2
s3
s4
s5
s6
输出key和value
s1 ['student1', 15, 'seat1', 158]
s2 ['student2', 16, 'seat2', 159]
s3 ['student3', 17, 'seat3', 160]
s4 ['stu4', 18, 's4', 170]
s5 ['stu5', 18, 's5', 171]
s6 ['stu6', 18, 's6', 171]
求长度
6
嵌套
{'s1': ['student1', 15, 'seat1', 158], 's2': ['student2', 16, 'seat2', 159], 's3': ['student3', 17, 'seat3', 160], 's4': ['stu4', 18, 's4', 170], 's5': ['stu5', 18, 's5', 171], 's6': ['stu6', 18, 's6', 171], 'qiantao': {'qiantao1': 1, 'qiantao2': 2}}
取值
{'qiantao1': 1, 'qiantao2': 2}
1
{'qiantao1': 1, 'qiantao2': 2}
2
Process finished with exit code 0