python列表
一、列表的创建与操作
1.创建列表
a = [] ##元素类型可为int,float,complex,str,list,tuple
b = [a, 1, True, 3j + 2, "hi"]
c = [[1, 2, 3, 4], [a, b], 233, "hello"]
d = [a, b, c ,'0.1']
print(a)
print(b)
print(c)
print(d)
[]
[[], 1, True, (2+3j), 'hi']
[[1, 2, 3, 4], [[], [[], 1, True, (2+3j), 'hi']], 233, 'hello']
[[], [[], 1, True, (2+3j), 'hi'], [[1, 2, 3, 4], [[], [[], 1, True, (2+3j), 'hi']], 233, 'hello'], '0.1']
2.列表的索引与切片:
c = [[1, 2, 3, 4], [5,6], 233, "hello"]
print(c)
print(type(c)) ##显示类型
print(c[0][-1])
print(c[-1][3])
print(c[-2])
[[1, 2, 3, 4], [5, 6], 233, 'hello']
<class 'list'>
4
l
233
3.强制转换
>>> s = list(range(5)) ##采用内置方法list()强制转换
>>> s
[0, 1, 2, 3, 4]
>>> s[3]
3
>>> s[0]
0
4.列表的重复
>>> list = [1,2,3,'hello']
>>> list*3
[1, 2, 3, 'hello', 1, 2, 3, 'hello', 1, 2, 3, 'hello']
5.列表的成员操作符
>>> list
[1, 2, 3, 'hello']
>>> 1 in list
True
>>> 'file' in list
False
6.列表的连接
>>> list
[1, 2, 3, 'hello']
>>> list2 = ['file','westos']
>>> list+list2
[1, 2, 3, 'hello', 'file', 'westos']
二、列表的编辑
1.添加元素 append 和 extend
append:添加元素,默认添加到最后
>>> list = [1,2,3,4,5]
>>> list.append('f')
>>> print(list)
[1, 2, 3, 4, 5, 'f']
extend :将新列表元素全部添加
>>> nal=['hello','file']
>>> list.extend(nal)
>>> list
[1, 2, 3, 4, 5, 'f', 'hello', 'file']
如果用append添加列表,则:
>>> list.append(nal) ```##将列表作为元素添加```
>>> list
[1, 2, 3, 4, 5, 'f', 'hello', 'file', ['hello', 'file']]
2.删除 remove 、pop 和 del(可删除列表) clear(清空列表)
remove:
>>> list.remove(1)
>>> list
[2, 3, 4, 5, 'f', 'hello', 'file', ['hello', 'file']]
pop:
>>> list
[2, 3, 4, 5, 'f', 'hello', 'file', ['hello', 'file']]
>>> list.pop(3) ##删除索引的元素,并返回索引值
5
>>> list
[2, 3, 4, 'f', 'hello', 'file', ['hello', 'file']]
del:
>>> list
[2, 3, 4, 'f', 'hello', 'file', ['hello', 'file']]
>>> del list[-1] ##删除指定元素
>>> list
[2, 3, 4, 'f', 'hello', 'file']
>>> del list[:-4] ##除了最后4的元素全删除
>>> list
[4, 'f', 'hello', 'file']
>>> del list[2:] ##从第2个索引开始全删除
>>> list
[4, 'f']
3.插入 insert
>>> list
[4, 'f']
>>> list.insert(1,'hello') ##插入到索引值前
>>> list
[4, 'hello', 'f']
4.统计次数 count
>>> list
[4, 'hello', 'f']
>>> list.count('f') ##统计f出现的次数
1
5.修改(重新赋值)
>>> list
[4, 'hello', 'f']
>>> list[1]='westos' ##索引修改
>>> list
[4, 'westos', 'f']
6.索引 index
>>> list
[4, 'westos', 'f']
>>> list.index('f') ##输入元素
2 ##输出对应的索引值
>>> list.index('westos')
1
7.排序 sort
>>> a = ['w','e','f','g']
>>> a.sort() ##正序,按ASCII码大小排列
>>> a
['e', 'f', 'g', 'w']
>>> a.sort(reverse=True) ##倒序,注意:reverse默认为False
>>> a
['w', 'g', 'f', 'e']
>>> a[::-1] ##倒序
['e', 'f', 'g', 'w']
随机排列
>>> import random
>>> a = [1,2,3,4,5,6]
>>> random.shuffle(a)
>>> a
[1, 4, 2, 6, 5, 3]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 6]
8.反转 reverse
>>> a = ['s','d','f','g','h']
>>> a.reverse()
>>> a
['h', 'g', 'f', 'd', 's']
9.复制 copy
练习:将用户列表与密码列表一一匹配登陆,若用户不存在、密码错误则提示报错;三次登陆机会!
while 1:
user = ['root','student','westos','file','w']
passwd = ['123','234','345','456','1128']
for i in range(3):
USER = input("请输入用户:")
# c = input("请输入密码:")
if USER in user:
PASS = input("请输入密码:")
d=user.index(USER)
if PASS == passwd[d]:
print("ok")
break
else:
print("密码不正确")
else:
print("没有" + USER + "用户")
else:
print("超过三次登陆!!")
break
请输入用户:file
请输入密码:file
密码不正确
请输入用户:hello
没有hello用户
请输入用户:westos
请输入密码:789
密码不正确
超过三次登陆!!
注意:若密码加密,需导入getpass模块,且该模块只能在命令行执行!!
三、构建队列与栈数据结构
1.简单队列结构构建(特点:先进先出)
queue = []
max_count = 10
menu = """
列队操作
1).入队
2).出队
3).队头
4).队尾
5).队列长度
6).队列元素显示
7).队列是否为空
8).队列是否满
请输入你的选择:"""
while 1:
choice = input(menu)
if choice == '1':
print("入队操作:".center(60, '*'))
if len(queue) < 10:
item = input("队列元素:")
queue.append(item)
print("%s入队成功" % (item))
else:
print("队列以满")
elif choice == '2':
if not queue:
print("对列为空")
else:
print("出队操作:".center(60, '*'))
item = queue.pop(0)
print("%s出队成功" % (item))
elif choice == '3':
if not queue:
print("对列为空")
else:
print(queue.index(0))
elif choice == '4':
if not queue:
print("对列为空")
else:
print(queue.index(-1))
elif choice == '5':
if not queue:
print("对列为空")
else:
print(len(queue))
elif choice == '6':
if not queue:
print("对列为空")
else:
for i in queue:
print(i, end=' ')
elif choice == '7':
if not queue:
print("对列为空")
else:
print("对列不为空")
elif choice == '8':
if len(queue) == max_count:
print("队列以满")
else:
print("队列未满")
else:
print("错误选择")