Chinaunix首页 | 论坛 | 博客
  • 博客访问: 877715
  • 博文数量: 339
  • 博客积分: 3151
  • 博客等级: 中校
  • 技术积分: 3425
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-10 14:47
文章分类

全部博文(339)

文章存档

2023年(43)

2022年(44)

2021年(3)

2020年(13)

2019年(39)

2018年(25)

2015年(2)

2014年(18)

2013年(12)

2012年(48)

2011年(79)

2010年(13)

分类: Python/Ruby

2022-12-08 14:02:58

Python 8天从入门到精通训练营  - 

数据类型-字典来啦



先在excel表里面
姓名, 年龄,座位号,身高,编辑出来,然后ctrl + E ,来生成一组。
student1 15 seat1 158 ["student1",15,"seat1",158],
student2 16 seat2 159 ["student2",16,"seat2",159],
student3 17 seat3 160 ["student3",17,"seat3",160],
student4 18 seat4 161 ["student4",18,"seat4",161],
student5 19 seat5 162 ["student5",19,"seat5",162],
student6 20 seat6 163 ["student6",20,"seat6",163],
student7 21 seat7 164 ["student7",21,"seat7",164],
student8 22 seat8 165 ["student8",22,"seat8",165],
student9 23 seat9 166 ["student9",23,"seat9",166],
student10 24 seat10 167 ["student10",24,"seat10",167],


student_list = [
["student1",15,"seat1",158],
["student2",16,"seat2",159],
["student3",17,"seat3",160],
["student4",18,"seat4",161],
["student5",19,"seat5",162],
["student6",20,"seat6",163],
["student7",21,"seat7",164],
["student8",22,"seat8",165],
["student9",23,"seat9",166],
["student10",24,"seat10",167]
]






#####命令#####

student_list = [
["student1",15,"seat1",158],
["student2",16,"seat2",159],
["student3",17,"seat3",160],
["student4",18,"seat4",161],
["student5",19,"seat5",162],
["student6",20,"seat6",163],
["student7",21,"seat7",164],
["student8",22,"seat8",165],
["student9",23,"seat9",166],
["student10",24,"seat10",167]
]

print(student_list)
print(student_list[0:])
print(student_list[0:-1])
print(i[-4:-1])
print(i[-1])




###输出结果####
"C:\Program Files\Python310\python.exe" "C:/Python/python8天从入门到精通/Day3/3.5 数据类型字典来了.py"
[['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['student4', 18, 'seat4', 161], ['student5', 19, 'seat5', 162], ['student6', 20, 'seat6', 163], ['student7', 21, 'seat7', 164], ['student8', 22, 'seat8', 165], ['student9', 23, 'seat9', 166], ['student10', 24, 'seat10', 167]]
[['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['student4', 18, 'seat4', 161], ['student5', 19, 'seat5', 162], ['student6', 20, 'seat6', 163], ['student7', 21, 'seat7', 164], ['student8', 22, 'seat8', 165], ['student9', 23, 'seat9', 166], ['student10', 24, 'seat10', 167]]
[['student1', 15, 'seat1', 158], ['student2', 16, 'seat2', 159], ['student3', 17, 'seat3', 160], ['student4', 18, 'seat4', 161], ['student5', 19, 'seat5', 162], ['student6', 20, 'seat6', 163], ['student7', 21, 'seat7', 164], ['student8', 22, 'seat8', 165], ['student9', 23, 'seat9', 166]]


Process finished with exit code 0



======================================================

student_list = [
["student1",15,"seat1",158],
["student2",16,"seat2",159],
["student3",17,"seat3",160],
["student4",18,"seat4",161],
["student5",19,"seat5",162],
["student6",20,"seat6",163],
["student7",21,"seat7",164],
["student8",22,"seat8",165],
["student9",23,"seat9",166],
["student10",24,"seat10",167]
]

Python 8天从入门到精通训练营
# print(student_list)
# print(student_list[0:])
# print(student_list[0:-1])
# for i in student_list:
#     # print(i[0:-1])
print(student_list[-4:-1])
print(student_list[-1])
print()
print(student_list[0])
print(student_list[0][0])
print(student_list[0][1])
print(student_list[0][2])

"C:\Program Files\Python310\python.exe" "C:/Python/python8天从入门到精通/Day3/3.5 数据类型字典来了.py"
[['student7', 21, 'seat7', 164], ['student8', 22, 'seat8', 165], ['student9', 23, 'seat9', 166]]
['student10', 24, 'seat10', 167]


['student1', 15, 'seat1', 158]
student1
15
seat1


Process finished with exit code 0




"C:\Program Files\Python310\python.exe" "C:/Python/python8天从入门到精通/Day3/3.5 数据类型字典来了.py"
[['student7', 21, 'seat7', 164], ['student8', 22, 'seat8', 165], ['student9', 23, 'seat9', 166]]
['student10', 24, 'seat10', 167]


['student1', 15, 'seat1', 158]
student1
15
seat1


Process finished with exit code 0

===========================
命令:
dic = {} 
print(dic)
输出:
{}

=================== 2022-12-08 13:44-

dic = {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159],
"s3":["student3",17,"seat3",160]
}
print("s1" in dic)

命令:
dic = {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159],
"s3":["student3",17,"seat3",160]
}
print("s1" in dic)

输出:
True

===================
存复杂的信息,然后快速的查询,这个就是字典的好处。
key value ,value后面可以跟任何值,跟一个字典都可以。 
key必须是字符串或者数字。 字符串和数字是不可变的。 key还必须是唯一的。 
value可以重复。 
字典是没有顺序的。

命令:

dic = {
"s1":["student1",15,"seat1",158],
"s2":["student2",16,"seat2",159],
"s2":["student",1,"seat",1],
"s3":["student3",17,"seat3",160]
}
print("s1" in dic)
print(dic["s2"]) ##输出key是s2的value。 key是重复的,第2个出来会把第一个覆盖。所以key要唯一。

输出: 
True
['student', 1, 'seat', 1]

字典的好处在于,有一个值,还是一亿个值,查询的速度几乎一样。 很神奇。 这就是算法的魅力。算法工程师就是解决效率的问题。 

下一节,学习字典的增删改查。



阅读(341) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~