内置方法练习:
1.去重下列列表并保留数据值原来的顺序
eg: [1,2,3,2,1] 去重之后 [1,2,3]
l1 = [2,3,2,1,2,3,2,3,4,3,4,3,2,3,5,6,5] [2, 3, 1, 4, 5, 6]
'''
lst = [1, 2, 3, 2, 1]
lst1 = [2, 3, 2, 1, 2, 3, 2, 3, 4, 3, 4, 3, 2, 3, 5, 6, 5]
# 用来接收lst1初次循环出来的数据
# lst2 = []
# print(lst1)
#
# print(list(set(lst)))
#
# 转集合自动去重,升序排列 解决办法有两种 sort()方法和sorted()方法
# print(list(set(lst1)).sort(key=lst1.index)) # sort() 方法没有返回值 返回None
# lst3 = sorted(list(set(lst1)), key=lst1.index)
# print(lst3)
# print(lst3)
# lst3.sort(key=lst1.index)
# print(lst3)
# sorted(lst3, key=lst1.index)
# print(lst3)
# lst3.sort(key=lst1.index)
# print(lst3)
# print(lst1)
# for i in lst1:
# if i not in lst2:
# lst2.append(i)
# print(lst2)
# 列表推导式 和for循环原理一样,都不会改变去重前的顺序
# [lst2.append(i) for i in lst1 if i not in lst2]
# print(lst2)
'''
2.有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={'jason','oscar','kevin','ricky','gangdan','biubiu'}
linuxs={'kermit','tony','gangdan'}
1. 求出即报名python又报名linux课程的学员名字集合
2. 求出所有报名的学生名字集合
3. 求出只报名python课程的学员名字
4. 求出没有同时这两门课程的学员名字集合
'''
pythons = {'jason', 'oscar', 'kevin', 'ricky', 'gangdan', 'biubiu'}
linuxs = {'kermit', 'tony', 'gangdan'}
# | 求所有的值,重复的只显示一个
# all_student = pythons | linuxs
# print(all_student)
#
# for i in pythons:
# print(i)
# if i in linuxs:
# only_pythons = pythons.remove(i)
# print(only_pythons)
# 1. 求出即报名python又报名linux课程的学员名字集合
# for i in pythons:
# for j in linuxs:
# if i == j:
# all_students = i
# print(i)
# all_student = pythons & linuxs
# print(all_student)
# 2. 求出所有报名的学生名字集合
# all_stdent = pythons | linuxs
# print(all_stdent)
# 3. 求出只报名python课程的学员名字
# python_student = pythons ^ linuxs
# print(python_student)
# 4. 求出没有同时这两门课程的学员名字集合
# all_student = pythons - linuxs
# all_student1 = linuxs - pythons
# all_student2 = all_student | all_student1
# print(all_student2)
# print(python_student)
'''
3.统计列表中每个数据值出现的次数并组织成字典战士
eg: l1 = ['jason','jason','kevin','oscar']
结果:{'jason':2,'kevin':1,'oscar':1}
真实数据
l1 = ['jason','jason','kevin','oscar','kevin','tony','kevin']
'''
l1 = ['jason', 'jason', 'kevin', 'oscar']
dict1 = {}
# count()方法可以统计数据值出现的次数
# print(l1.count('jason'))
# for i in l1:
# print(i)
# dict1[i] = l1.count(i)
# print(dict1)
# print(dict1)
标签:内置,jason,lst1,练习,linuxs,print,lst3,方法,pythons
From: https://www.cnblogs.com/HaiMan/p/16756001.html