一、列表
1、列表的创建
a = [] ##创建一个空列表
b = ['hello',123,True,[1,2,3]] ##列表里面的元素可以是任何类型
print(a,type(a))
print(b,type(b))
2、列表的特性
1、索引
b = ['hello',123,True,[1,2,3]]
print(b[3])
print(b[3][1])
2、切片
3.强制转换
4.列表的重复
5.列表的成员操作符
6.列表的连接
>>>l
[1, 2, 3, True]
>>>l1
[3, 2, 1, False]
>>>l+l1
[1, 2, 3, True, 3, 2, 1, False]
3、列表的编辑
1.添加元素 append 和 extend
append 添加单个元素,默认添加到末尾
>>>l = ['a','b','c']
>>>l.append('d')
>>>print(l)
['a', 'b', 'c', 'd']
extend添加多个元素到列表
>>>l
['a', 'b', 'c', 'd']
>>>l1 = ['e','f']
>>>l.extend(l1)
>>>l
['a', 'b', 'c', 'd', 'e', 'f']
如果用append添加列表,则添加成一个元素
2.删除 remove 、pop 和 del(可删除列表) clear(清空列表)
remove删除指定元素
l = ['a','b','c']
l.remove('c')
print(l)
pop以索引值的方式删除元素,并返回索引值
>>>l = ['a','b','c']
>>>l.pop(1)
'b'
>>>l
['a', 'c']
del按照索引和切片删除
>>>l = ['a','b','c','d','e']
>>>del l[-1] #删除最后一个元素
>>>l
['a', 'b', 'c', 'd']
>>>del l[:4] #删除索引4前的所有元素
>>>l
[]
clear清空
>>>l = [1,2,3,4,5,6,7]
>>>l.clear()
>>>l
[]
3、insert插入
>>>l = [1,2,3,4,5]
>>>l.insert(5,6) 插入6这个元素到5索引的位置
>>>l
[1, 2, 3, 4, 5, 6]
4、count统计次数
>>>l = ['a','b','c','a','b']
>>>l.count('a') ##统计元素a出现的字数
2
5、修改
>>>l = [1,2,3,4,5]
>>>l[0] = 0 ##修改索引值为0的元素
>>>l
[0, 2, 3, 4, 5]
6、index索引
>>>l
[0, 2, 3, 4, 5]
>>>l.index(0) ##查看被0元素的索引值
0
>>>l.index(5) ##查看5元素的索引值
4
7、排序sort
l = ['r','f','j','k']
l.sort() ##按照ascii码大小排序
l
['f', 'j', 'k', 'r']
l.sort(reverse=True) ##倒序
l
['r', 'k', 'j', 'f']
l[::-1] ##倒序
['f', 'j', 'k', 'r']
随机排序
>>>import random ##导入ramdom模块
>>>a = [1,2,3,4,5,6,7,8,9]
>>>random.shuffle(a) ##随机【排序
>>>a
[6, 5, 4, 2, 3, 9, 8, 1, 7]
>>>a.sort()
a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
8.反转 reverse
>>>a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>a.reverse()
>>>a
[9, 8, 7, 6, 5, 4, 3, 2, 1]
9.复制 copy
>>>l = [[1,2,3],1,2]
>>>import copy 导入复制模块
>>>l1 = copy.copy(l) 浅copy
>>>l1
[[1, 2, 3], 1, 2]
>>>id(l[0]),id(l1[0]) 浅copy列表id相同
(140364930217928, 140364930217928)
>>>id(l),id(l1)
(140364980008648, 140364924607112)
l2 = copy.deepcopy(l) 深copy中列表元素id不通
l2
[[1, 2, 3], 1, 2]
id(l),id(l2)
(140364980008648, 140364939560072)
id(l[0]),id(l2[0])
(140364930217928, 140364924643592)
练习:将用户列表与密码列表一一匹配登陆,若用户不存在、密码错误则提示报错;三次登陆机会!
user = ['root','student','westos']
passwd = ['123','234','345']
for i in range(3):
User = input("用户名:")
Pass = input("密码:")
if User in user:
n = user.index(User)
if Pass ==passwd[n]:
print("登陆成功!")
break
else:
print("密码错误!")
else:
print(User+"用户不存在")
continue
else:
print("Error:登陆机会超过三次")
注意:若密码加密,需导入getpass模块,且该模块只能在命令行执行!!
二、队列数据结构和栈数据结构
1、队列数据结构
队列管理练习
queue = []
max_count = 10
menu = """
队列操作
1). 入队
2). 出队
3). 队头
4). 队尾
5). 队列长度
6). 队列元素显示
7). 队列是否为空: empty
8). 队列是否满 full
请输入你的选择:"""
choice = input(menu)
if choice == '1':
print("入队操作".center(40, '*'))
if len(queue) < 10:
item = input("入队元素:")
queue.append(item)
print("%s入队成功!" % (item))
else:
print('队列已满!')
elif choice == '2':
print("出队操作".center(40,'*'))
# 如何判断列表是否为空(3种方式)
# if len(queue) == 0:
# if queue == []:
if not queue:
print("队列为空")
else: # 队列是先进先出, 出队的是第一个元素;
item = queue.pop(0)
print("%s出队成功" % (item))
elif choice == '3':
print("队头".center(40,'*'))
if not queue:
print("队列为空")
else:
print("队头为:"+queue[0])
elif choice =='4':
print("队尾".center(40,'*'))
if not queue:
print("队列为空")
else:
print("队尾为:"+queue[-1])
elif choice == '5':
print("队列长度".center(40, '*'))
print("队列长度为:"+len(queue))
elif choice =='6':
print("队列展示".center(40,'*'))
if not queue:
print("队列为空")
else:
print("队列元素有:",end = ' ')
for i in queue:
print(i,end = ' ')
elif choice == '7':
if not queue:
print("队列为空")
else:
print("队列不为空")
elif choice == '8':
if len(queue) < 10:
print("队列未满")
else:
print("队列已满")
else:
print("Error:错误的操作!!")
2、栈数据结构
queue = []
max_count = 10
menu = """
栈操作
1). 入栈
2). 出栈
3). 栈头
4). 栈尾
5). 栈长度
6). 栈元素显示
7). 栈是否为空: empty
8). 栈是否满 full
请输入你的选择:"""
choice = input(menu)
if choice == '1':
print("入栈操作".center(40, '*'))
if len(queue) < 10:
item = input("入栈元素:")
queue.append(item)
print("%s入栈成功!" % (item))
else:
print('栈已满!')
elif choice == '2':
print("出栈操作".center(40,'*'))
# 如何判断栈是否为空(3种方式)
# if len(queue) == 0:
# if queue == []:
if not queue:
print("栈为空")
else: # 栈结构是先进后出, 出队的是最后一个元素;
item = queue.pop(-1)
print("%s出栈成功" % (item))
elif choice == '3':
print("队头".center(40,'*'))
if not queue:
print("栈为空")
else:
print("栈头为:"+queue[0])
elif choice =='4':
print("栈尾".center(40,'*'))
if not queue:
print("栈为空")
else:
print("栈尾为:"+queue[-1])
elif choice == '5':
print("栈长度".center(40, '*'))
print("栈长度为:"+len(queue))
elif choice =='6':
print("栈展示".center(40,'*'))
if not queue:
print("栈为空")
else:
print("栈元素有:",end = ' ')
for i in queue:
print(i,end = ' ')
elif choice == '7':
if not queue:
print("栈为空")
else:
print("栈不为空")
elif choice == '8':
if len(queue) < 10:
print("栈未满")
else:
print("栈已满")
else:
print("Error:错误的操作!!")
三、拓展
1、is和==的区别
is:数据类型,大小,节点(地址)必须一致!
== 数据类型,大小一致就行,(浅copy的两个列表是==,深copy是is)
四、元组
1、元组的创建
>>>t = (1,2,3,4) ##元组创建
>>>type(t)
<class 'tuple'>
>>>a = (1) ###元组创建时要加,否则不是元组类型
>>>type(a)
<class 'int'>
>>>a = (1,)
>>>type(a)
<class 'tuple'>
2、元组的特性
元组支持的特性有:索引,切片,重复,成员操作符和for循环
操作方式与列表相同
3、元组的复制
t = ('python','shell')
print("hello %s,hello %s" %t)
hello python,hello shell
x,y = 10,20
x,y = y,x ##在其他语言中,必须要有中间值来传递,python中不需要
x,y
(20, 10)
4、列表的排序与掐头去尾
>>>s = [89,77,94,85,99]
>>>s.sort()
>>>s
[77, 85, 89, 94, 99]
>>>min_s,*middle,max_s=s
>>>min_s
77
>>>max_s
99
>>>sum(middle)
268
此方式在python2中不适用!!
五、集合
注意:定义空集合:set()!!
1.交集
a = {1, 2}
b = {1, 2, 3, 4}
print(a & b)
print(a.intersection(b))
print(a.isdisjoint(b)) ##判断a和b有没有交集,没有为True,否则为False;
2.合集
a = {1, 2}
b = {3, 4}
print(a | b ) ##两种方式均可
print(a.union(b))
3.差集
a = {1, 2}
b = {1,2,3, 4}
print(b - a) ##两种方式均可
print(b.difference(a))
4.对差等分:列举两集合不同的元素
a = {1, 2,3}
b = {1,3,4}
print(a^b) ##结果{2,4},两种方式均可
print(a.symmetric_difference(b))
5.子集、父集
a = {1, 2,}
b = {1,2,3,4}
print(a.issubset(b)) ##判断a是b的子集吗?是则返回True,否则返回False
print(b.issuperset(a)) ##判断b是a的父集吗?是则返回True,否则返回False