数据类型之列表
列表
如果想要表示两个同学 users = "李邵,李奇航...".
以后想要表示多个”事物“,可以使用列表。
如果想要表示两个同学 users = "李邵,李奇航...".
以后想要表示多个”事物“,可以使用列表。
1.append,在列表的最后追加一个元素
示例
users = []
users.append('a')
print(users)
users = []
users.append('a')
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['a']
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['a']
2.insert在指定元素前面插入元素
示例:
users = ['张三','李四','王五']
users.insert(1,'小明') #在第二个元素前面添加元素
print(users)
示例:
users = ['张三','李四','王五']
users.insert(1,'小明') #在第二个元素前面添加元素
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', '小明', '李四', '王五']
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', '小明', '李四', '王五']
3.remove/clear/pop/del删除指定元素
示例:
users = ['张三','李四','王五']
users.remove('张三') #指定元素的字符串
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['李四', '王五']
#方式二:
users = ['张三','李四','王五']
users.clear()
print(users)
users = ['张三','李四','王五']
users.clear()
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
[]
[]
4.reverse反转
示例:
v1 = [1,2,3111,32,13]
print(v1)
v1.reverse()
print(v1)
print(v1)
v1.reverse()
print(v1)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
[1, 2, 3111, 32, 13]
[13, 32, 3111, 2, 1]
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
[1, 2, 3111, 32, 13]
[13, 32, 3111, 2, 1]
示例:
v1 = [1,2,3111,32,13]
v1.sort(reverse=False) #默认从小到大排序
print(v1)
v1.sort(reverse=True) #从大到小排序
print(v1)
v1.sort(reverse=False) #默认从小到大排序
print(v1)
v1.sort(reverse=True) #从大到小排序
print(v1)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
[1, 2, 13, 32, 3111]
[3111, 32, 13, 2, 1]
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
[1, 2, 13, 32, 3111]
[3111, 32, 13, 2, 1]
公共功能
1.len(列表长度)
示例:
users = ["李四","张三",66]
user_len = len(users)
print(user_len)
users = ["李四","张三",66]
user_len = len(users)
print(user_len)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
3
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
3
2.索引
示例:
users = ["李四","张三",66]
users_index = users[0]
print(users_index)
users = ["李四","张三",66]
users_index = users[0]
print(users_index)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
李四
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
李四
3.切片
示例:
users = ["李四","张三",66]
users_1 = users[1:3]
print(users_1)
users = ["李四","张三",66]
users_1 = users[1:3]
print(users_1)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', 66]
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', 66]
4.删除(数字/布尔/字符串除外)
示例:
方式一:
users = ['张三','李四','王五']
a = users.pop(0)
print(a)
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
张三
['李四', '王五']
示例:
方式一:
users = ['张三','李四','王五']
a = users.pop(0)
print(a)
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
张三
['李四', '王五']
# 方式二
示例:
users = ['张三','李四','王五']
del users[0]
print(users)
users = ['张三','李四','王五']
del users[0]
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['李四', '王五']
['李四', '王五']
del和.pop区别:
del users[0] 仅代表删除
del users[0] 仅代表删除
a = users.pop(0) 在列表中删除,并将删除的此数据赋值a
5.修改(字符串/数字/布尔除外
注意: 字符串本身不能修改或删除【不可变类型】 v1 = "alex".upper() 列表是可变类型。
示例:
users = ['张三','李四','王五']
users[1] ='小明'
print(users)
注意: 字符串本身不能修改或删除【不可变类型】 v1 = "alex".upper() 列表是可变类型。
示例:
users = ['张三','李四','王五']
users[1] ='小明'
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', '小明', '王五']
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['张三', '小明', '王五']
6.步长:
示例:
users = ["李四","张三",66,88,999]
users_1 = users[0:5:2]
print(users_1)
users_1 = users[0:5:2]
print(users_1)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['李四', 66, 999]
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['李四', 66, 999]
7.for循环
实现一个整数加法计算器(两个数相加):
如:content = input("请输入内容:")
用户输入:5+9或5+ 9或5 + 9(含空白),然后进行分割转换最 终进行整数的计算得到结果。
"""
# 思路一:
""" content = input('请输入:')
# [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
content = content.strip()
# [5+9] 或 [5 +9] 或者 [5 + 9]
v1 = int(content[0])
v2 = int(content[-1])
v3 = v1 + v2 """
# 思路二:
""" content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
content_len = len(content)
index = 0
total = 0
while True:
char = content[index]
if char.isdigit():
total += int(char)
index += 1
if index == content_len:
break
print(total)
"""
# 思路三:
"""
content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
result = content.split('+')
# print(result) # ['55 ', ' 99 ']
v1 = int(result[0]) # "55"
v2 = int(result[1]) # " 99 "
v3 = v1 + v2
实现一个整数加法计算器(两个数相加):
如:content = input("请输入内容:")
用户输入:5+9或5+ 9或5 + 9(含空白),然后进行分割转换最 终进行整数的计算得到结果。
"""
# 思路一:
""" content = input('请输入:')
# [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
content = content.strip()
# [5+9] 或 [5 +9] 或者 [5 + 9]
v1 = int(content[0])
v2 = int(content[-1])
v3 = v1 + v2 """
# 思路二:
""" content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
content_len = len(content)
index = 0
total = 0
while True:
char = content[index]
if char.isdigit():
total += int(char)
index += 1
if index == content_len:
break
print(total)
"""
# 思路三:
"""
content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
result = content.split('+')
# print(result) # ['55 ', ' 99 ']
v1 = int(result[0]) # "55"
v2 = int(result[1]) # " 99 "
v3 = v1 + v2
8.列表循环
示例:
name = ['张三','李四','王五','刘六']
for i in name:
for n in i:
print(n)
print(i)
name = ['张三','李四','王五','刘六']
for i in name:
for n in i:
print(n)
print(i)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
张
三
张三
李
四
李四
王
五
王五
刘
六
刘六
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
张
三
张三
李
四
李四
王
五
王五
刘
六
刘六
# 练习题:请通过for循环和数字计数器实现:
# users = ['张三','李四','王五','刘六']
# 0 张三
# 1 李四
# 2 王五
# 3 刘六
name = ['张三','李四','王五','刘六']
name_len=len(name)
for i in range(0,name_len):
print(i,name[i])
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
0 张三
1 李四
2 王五
3 刘六
# users = ['张三','李四','王五','刘六']
# 0 张三
# 1 李四
# 2 王五
# 3 刘六
name = ['张三','李四','王五','刘六']
name_len=len(name)
for i in range(0,name_len):
print(i,name[i])
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
0 张三
1 李四
2 王五
3 刘六
##################用户名和密码校验####################
#录入用户和密码
users = []
for i in range(0,3):
name = input('请输入你的名字和密码:')
users.append(name)
print(users)
#录入用户和密码
users = []
for i in range(0,3):
name = input('请输入你的名字和密码:')
users.append(name)
print(users)
#用户校验
user = input('请输入你账号,密码:')
result = user.split(',')
print(result)
for i in users:
print(i)
a = i.split(',')
print(a)
if a[0] == result[0] and a[1] == result[1]:
print('登陆成功')
break
user = input('请输入你账号,密码:')
result = user.split(',')
print(result)
for i in users:
print(i)
a = i.split(',')
print(a)
if a[0] == result[0] and a[1] == result[1]:
print('登陆成功')
break
print('账号或密码不正确')
9.extend循环将字符串,元组或者列表中字符加入列表中
#示例:
users = ['sundy','哈哈','123',23]
s = 'abc'
users.extend(s)
print(users)
users = ['sundy','哈哈','123',23]
s = 'abc'
users.extend(s)
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['sundy', '哈哈', '123', 23, 'a', 'b', 'c']
users = ['sundy','哈哈','123',23]
s = ['amy','abs',666]
users.extend(s)
print(users)
s = ['amy','abs',666]
users.extend(s)
print(users)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['sundy', '哈哈', '123', 23, 'amy', 'abs', 666]
D:\python3.6\python3.6.exe D:/python_code/day01/day04.py
['sundy', '哈哈', '123', 23, 'amy', 'abs', 666]
注意:元组中不能使用extend(将字符串,元组或者列表中字符加入元组中)
10.join列表元素拼接(字符串,列表,元组适用)
示例:
users = ['sundy','amy','小明']
results = ",".join(users)
print(results)
results = ",".join(users)
print(results)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
sundy,amy,小明
sundy,amy,小明
总结:
增:
append / insert
删:
remove / pop / clear / del users[2]
改:
users[3] = "新值"
查:
索引/切片
增:
append / insert
删:
remove / pop / clear / del users[2]
改:
users[3] = "新值"
查:
索引/切片
列表嵌套
users = ["alex",0,True,[11,22,33,"老男孩"],[1,['alex','oldboy'],2,3]]
users[0]
users[2]
users[0][2]
users[3] # [11,22,33,"老男孩"]
users[3][-1] # "老男孩"
users[3][-1][1] # '男'
users[3] = 666
users[0]
users[2]
users[0][2]
users[3] # [11,22,33,"老男孩"]
users[3][-1] # "老男孩"
users[3][-1][1] # '男'
users[3] = 666
练习:
# 让用户输入任意字符串,然后判断此字符串是否包含指定的敏感字符。
char_list = ['利奇航','堂有光','炸展会']
content = input('请输入内容:') # 我叫利奇航 / 我是堂有光 / 我要炸展会
success = True
for v in char_list:
if v in content:
success = False
break
if success:
print(content)
else:
print('包含铭感字符')
char_list = ['利奇航','堂有光','炸展会']
content = input('请输入内容:') # 我叫利奇航 / 我是堂有光 / 我要炸展会
success = True
for v in char_list:
if v in content:
success = False
break
if success:
print(content)
else:
print('包含铭感字符')