一、列表
(1)添加数据
①append()
list_1 = [1,2,3,4,5]
list_1.append(6) # 在列表的末尾添加数据
print(list_1)
————>
[1, 2, 3, 4, 5, 6]
②insert()
list_2 = ['a','b','c','d']
list_2.insert(1,'s') #根据索引在指定的1位置添加数据
print(list_2)
————>
['a', 's', 'b', 'c', 'd']
(2)查询修改数据
①根据索引
list_3 = [1,2,3,4,5,6,7]
#查询
list_3[0]
print(list_3[0])
#修改
list_3[0] = 2
print(list_3)
————>
1 [2, 2, 3, 4, 5, 6, 7]
②切片
list_4 = [1,2,3,4,5,6,7,8,9]
# 查询元素
list_4[0:3]
print(list_4[0:3])
# 修改元素
list_4[0:3] = [0,0,0]
print(list_4)
—————>
[1, 2, 3] [0, 0, 0, 4, 5, 6, 7, 8, 9]
(3)删除数据
①pop()
list_5 = ['a','b','c','d','e']
list_5.pop(1)
list_5.pop() # pop()是基于索引删除的一种方法,如果不给定()中的值,默认删除最后一位。
print(list_5)
————>
['a', 'c', 'd']
②remove()
list_6 = ['a','b','c','c','c','d','e','f']
list_6.remove('c') # remove()是根据具体给定元素删除的一种方法
# 需要注意的是: 当表中出现多个重复的元素时,只删除第一个匹配的值。
print(list_6)
————>
['a', 'b', 'c', 'c', 'd', 'e', 'f']
③del 搭配索引使用
list_7 = ['a','b','c','d','e','f','g']
del list_7[2:4]
print(list_7)
del list_7 # 当del 删除列表时,后面附加切片索引,可以删除索引的对象
# 而后面不附加索引时,就会直接删除这个列表
————>
['a', 'b', 'e', 'f', 'g']
④clear ()
list_8 = ['a','b','c','d','e','f','g','h']
list_8.clear() # clear () 为清空列表中全部元素,不删除列表
print(list_8)
————>
[]
(4)列表的合并
①直接加和
list_1 = [1,2,3,4,5]
list_2 = [5,6,7,8]
list = list_1 + list_2 #直接将两个列表相加
print(list)
————>
[1, 2, 3, 4, 5, 5, 6, 7, 8]
②extend()
list_1 = [1,2,3,4,5]
list_2 = [5,6,7,8]
list_1 .extend(list_2) #将list_2扩展给list_1
print(list_1)
————>
[1, 2, 3, 4, 5, 5, 6, 7, 8]
(5) 列表的排序
①升序
list_9 = [1,4,3,7,8,9,5,6]
list_9.sort(reverse = False) # sort()默认表示reverse=False将列表中的数值按升序排列
print(list_9)
————>
[1, 3, 4, 5, 6, 7, 8, 9]
②降序
list_9 = [1,4,3,7,8,9,5,6]
list_9.sort(reverse = True) # 将列表中的元素降序排列
print (list_9)
————>
[9, 8, 7, 6, 5, 4, 3, 1]
(6) 列表的复制
list_a = [1,2,3,4,5]
list_copy = list_a.copy()
(7)列表中元素的合并输出
words_1 = ['i','love','you']
print('—'.join(word)) # 以 . 前面‘’中的符号分割 可以是各种符号,空格等
————>
i—love—you
words_2 = ['我玩打野','我玩中路','我玩游走','我补位']
print('\n'.join(words))
————>
我玩打野 我玩中路 我玩游走 我补位
(8)将字符串拆分成列表
words_1 = '我是leo,我23岁,我喜欢Python'
# 这里要特别注意的是: slipt()由字符串的分割符号拆分 (注意输入法中\英的区别)
sep_words = words_1.split(',')
print(sep_words)
————>
['我是leo','我23岁','我喜欢Python']
(9)列表的实例运用
使用列表和循环条件语句筛选不同性别、和年龄大于20岁的人
all_people = [
['leo','男',23],
['make','男',24],
['mar','男',20],
['hecy','女',20],
['kaliy','女',23]
]
male = []
female = []
age_beyong_20 = []
for people in all_people:
if people[1] == '男':
male.append(people[0])
else:
female.append(people[0])
if people[2] > 20:
age_beyong_20.append(people[0])
print(female)
print(male)
print(age_beyong_20)
————>
['hecy', 'kaliy'] ['leo', 'make', 'mar'] ['leo', 'make', 'kaliy']
二、元组
①单值创建元组时,数据后面必须有 ,
tuple_2 = ('a',)
②元组通过变量接值
tuple_3 = ('leo',23,1.72)
name,age,city = tuple_3
print(name,age,hight)
————>
leo 23 1.72
③tuple_3.count('leo') # 利用 tuple.count('a') 查询元组中“a”的数量)
————>
1
③元组创建后不可修改,可以通过索引、切片、遍历访问元组
(2)利用元组和列表以及循环语句写一个简单的筛选简历的程序
要求:
本科以上学历
30岁以下
优先选择211,985院校
job_seeker=[
('a','男',23,'本科','211 '),
('b','男',22,'本科','双非'),
('c','女',27,'硕士','985'),
('d','男',32,'博士','211'),
('e','女',22,'本科','985'),
('f','男',23,'本科','双非'),
('g','女',22,'大专','双非'),
('h','男',30,'硕士','985'),
('i','男',23,'本科','211'),
('j','女',24,'本科','双非')
]
interview = []
candidate = []
talent_pool = []
standard = (('本科','硕士','博士'),30,('211','985'))
for people in job_seeker:
if peo[3] in standard[0]:
talent_pool.append(peo[0])
if peo[2] < standard[1]:
candidate.append(peo[0])
if peo[4] in standard[2] :
interview.append(peo[0])
else:
print(peo[0],'您不满足我们的要求!')
print(f'进入面试的有{len(interview)}位,分别是:')
print('\n'.join(interview))
print('进入候选的有%d位,分别是:'%len(candidate))
print('\n'.join(candidate))
print('进入人才库的有{0}位,分别是:'.format(len(talent_pool))) # 这里复习了之前的三种占位方法
print('\n'.join(talent_pool))
————>
g 您不满足我们的要求!
进入面试的有4位,分别是: a c e i 进入候选的有7位,分别是: a b c e f i j 进入人才库的有9位,分别是: a b c d e f h i j
三、字典
(1)字典的创建
①dict_1 = {'姓名':'leo','年龄':30,'性别':'男'}
②dict_2 = dict(姓名='leo',年龄=23,性别='男')
'姓名' : 'leo'
键(key) 值(value)
(2)字典的复制及遍历
dict_3 = dict_1.copy()
for k,v in dict_3.items(): #键值对的遍历
for k in dict_3.keys(): #键的遍历
for v in dict_3.values(): #值的遍历
(3)字典的合并
dict_3 = {'姓名':'leo','年龄':30,'性别':'男'}
dict_4 = {'姓名':'hecy','专业':'化工'}
# 这里的语法是a.update(b),将b中有,a中没有的键值对添加给a, #将ab中重复的键,但不同值的会更新
dict_3.update(dict_5)
print(dict_3)
————>
{'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工'}
(5)字典的查询
①setdefault()
dict_5 = {'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工'}
dict_3.setdefault('姓名')
————>
hecy
#查询的有则输出,没有就会添加到字典中
dict_3.setdefault('对象','leo') #有则输出,无则添加,用键值逗号相隔
print(dict_3)
————>
{'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工', '对象': 'leo'}
#如果不给值,只有键,也会输出,只不过值为None
————>
{'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工', '对象': None}
②get 方法查询,有则输出,没有则输出 None,可以做条件判断。
print(dict_3.get('配偶'))
if dict_3.get('配偶') == None:
print('没找到')
————>
None 没找到
print(dict_3.get('专业'))
————>
化工
(6)字典的添加
dict_3 = {'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工', '对象': 'None'}
dict_3['政治面貌'] = '团员' #添加数据
print(dict_3)
dict_3['姓名'] = 'leo' #不仅可以用作添加,也可以修改数据
print(dict_3)
{'姓名': 'hecy', '年龄': 23, '性别': '男', '专业': '化工', '对象': None, '政治面貌': '团员'} {'姓名': 'leo', '年龄': 23, '性别': '男', '专业': '化工', '对象': None, '政治面貌': '团员'}
(7) 字典的删除
①pop()
dict_3 = {'姓名': 'leo', '年龄': 23, '性别': '男', '专业': '化工', '对象': None, '政治面貌': '团员'}
dict_3.pop('政治面貌') # 删除指定的键值对
print(dict_3)
————>
{'姓名': 'leo', '年龄': 23, '性别': '男', '专业': '化工', '对象': None}
②clear()
dict_1.clear() #清空所有的kv对
print(dict_1)
————>
[]
③popitem()
dict_3.popitem() # .popitem() 删除最后添加的kv
print(dict_3)
————>
{'姓名': 'leo', '年龄': 23, '性别': '男', '专业': '化工', '对象': None}
(8)字典实例
people_1 = {'姓名':leo','年龄':25,'性别':'男','分数':467}
people_2 = {'姓名':'hecy','年龄':23,'性别':'女,'分数':435}
people_3 = {'姓名':'mary','年龄':28,'性别':'女','分数':423}
interviewee=[people_1,people_2,people_3]
for people in interviewee:
if interviewee['分数'] >=450: # 字典没有索引,可用key可索引
print(interviewee['姓名'])
————>
leo