===================
python列表练习题
===================
一、创建列表
1.创建一个列表,命名为names,往里面添加陈贤贤、大财神、飞鱼、WuYing、阿阳和Black元素。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print(names)
结果[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
2.列表文字的添加
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print('我建的学习群有', len(names), '个学生在微信群里面')
print('我建的学习群有' + str(len(names)) + '个学生在微信群里面')
for item in names:
print(names)
print('我在渡劫也加入了微信群')
names.append('我在渡劫')
print('现在我的微信群是', names)
结果:
我建的学习群有 6 个学生在微信群里面 #6的前后都有空格存在
我建的学习群有6个学生在微信群里面 # 6个前后没有空格存在
陈贤贤
大财神
飞鱼
WuYing
阿阳
Black
我在渡劫也加入了微信群
现在我的微信群是 [‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’, ‘我在渡劫’]
二、在列表中插入元素
1.往names列表中的最后一个元素Black前面插入一个张四岁。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.insert(-1, "张四岁")
print(names)
结果[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘张四岁’, ‘Black’]
2.往names列表中最前面的元素陈贤贤前面插入张四岁
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.insert(0, '张四岁')
print(names)
结果[‘张四岁’, ‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
3.往names列表中中间部位的元素飞鱼前面插入张四岁
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.insert(2, '张四岁')
print(names)
结果[‘陈贤贤’, ‘大财神’, ‘张四岁’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
4.往names列表中大财神后面插入一个子列表[“老男孩”,“老女孩”]。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.insert(2, ["老男孩", "老女孩"])
print(names)
结果:[‘陈贤贤’, ‘大财神’, [‘老男孩’, ‘老女孩’], ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
三、改变列表中的某一个元素的名称
1.把names列表中WuYing的名字改成中文。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names[names.index("WuYing")] = "吴莹"
print(names)
结果:[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘吴莹’, ‘阿阳’, ‘Black’]
2.把names列表中飞鱼的名字改成数字666
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names[names.index('飞鱼')] = 666
print(names)
结果:[‘陈贤贤’, ‘大财神’, 666, ‘WuYing’, ‘阿阳’, ‘Black’]
3.修改特定元素
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names[3] = "吴莹"
print(names)
结果:[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘吴莹’, ‘阿阳’, ‘Black’]
4.批量修改元素名
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names[0:2] = 'abcd' # 将索引0-2替换为abcd,切片之后迭代处理
print(names)
结果:[‘a’, ‘b’, ‘c’, ‘d’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
四、确定列表中元素的位置(索引值)
1.返回names列表中阿阳的索引值(下标)。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print(names.index("阿阳"))
结果:4
五、两个列表的合并
1.创建新列表numbers,依次包含1,2,3,4,2,5,6,2等数,并把新列表合并到names列表中。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
numbers = [1, 2, 3, 4, 2, 5, 6, 2]
names.extend(numbers) # extend()方法表示合并
print(names)
结果:[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’, 1, 2, 3, 4, 2, 5, 6, 2]
六、取出列表中指定元素
1.取出names列表中索引4-7的元素。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black", 1, 2, 3, 4, 2, 5, 6, 2]
print(names[4:8])
结果:[‘阿阳’, ‘Black’, 1, 2]
2.取出names列表中索引2-10的元素,步长为2。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black", 1, 2, 3, 4, 2, 5, 6, 2]
print(names[2:11:2]) # 列表切片“顾头不顾尾”,步长表示在指定范围间隔取值
#结果:[‘飞鱼’, ‘阿阳’, 1, 3, 2]
3.取出names列表中最后3个元素。
names = ['陈贤贤', '大财神', ['老男孩', '老女孩'], '飞鱼', '吴莹', '阿阳', '张四岁', 'Black', 1, 2, 3, 4, 2, 5, 6, 2]
print(names[-3:]) # [-3:]表示取值范围为从列表的倒数第三个到末尾
结果:[5, 6, 2]
七、打印特定索引值和元素
1.循环names列表,打印每个元素的索引值和元素。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
方法1
for i in names:
print(names.index(i), i)
方法2 enumerate()枚举
for index, i in enumerate(names):
print(index, i)
结果:
0 陈贤贤
1 大财神
2 飞鱼
3 WuYing
4 阿阳
5 Black
2.循环names列表,打印每个元素的索引值和元素,当索引值为偶数时,把对应的元素改成-1。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black", 1, 2, 3, 4, 2, 5, 6, 2]
for index, i in enumerate(names):
if index % 2 == 0:
names[index] = -1
print(index, i)
print(names)
结果:[-1, ‘大财神’, -1, ‘WuYing’, -1, ‘Black’, -1, 2, -1, 4, -1, 5, -1, 2]
3.names列表里有3个2,请返回第二个2的索引值,不要人肉,要动态找。
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black", 1, 2, 3, 4, 2, 5, 6, 2]
方法1 循环
count = 0
for index, i in enumerate(names):
# print(index,i)
if i == 2:
count += 1
while count == 2:
print(index)
break
else:
continue
方法2
print(names.index(2, names.index(2)+1))
结果:10
4.现有商品列表如下:
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
需打印出以下格式:
------ 商品列表 ------
0 iphone 6888
1 MacPro 14800
2 小米6 2499
3 Coffee 31
4 Book 60
5 Nike 699
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
print("--------- 商品列表 --------")
for index, i in enumerate(products):
print("%s %s %s" % (index, i[0], i[1]))
结果:
--------- 商品列表 --------
0 iphone 6888
1 MacPro 14800
2 小米6 2499
3 Coffee 31
4 Book 60
5 Nike 699
5.根据products列表写一个循环,不断询问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,最终用户输入q退出时,打印购买的商品列表。
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
shop_car = [] # 用户购物车
shop_cost = 0 # 用户花费的金额
exit_log = False # 标志位,默认设为False,退出
while not exit_log:
print("------ 商品列表 ------")
for index, i in enumerate(products):
print("%s %s %s" % (index, i[0], i[1]))
user_choice = input("\n输入你想购买的产品序号(按“q”退出):")
if user_choice.isdigit():
# 判断用户输入的是否是数字
user_choice = int(user_choice) # 强制转换为数字
if user_choice >=0 and user_choice < len(products):
# 判断用户购买的商品是否在商品列表中
shop_car.append(products[user_choice]) # 加入购物车
shop_cost += products[user_choice][1] # 计算费用
print("\n %s 已经加入你的购物车\n"%products[user_choice])
else:
print("抱歉,此商品不存在\n")
elif user_choice == "q":
# 用户选择退出
if len(shop_car) > 0:
# 判断用户是否购买了商品
print("\n------ 你的购物车 ------")
for index, i in enumerate(shop_car):
# index和i为临时变量,与前一个for循环里index和i作用的列表不同,故可重用
print("%s %s" % (i[0], i[1]))
print("\n你此次购物的花费合计是:%s元\n" % shop_cost)
exit_log = True # 退出购物
else:
exit_log = True # 未购买商品,不打印购物车商品,直接退出
else:
# 输入不合法
exit_log = True
6.打印列表本身
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print(names)
结果:[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
7.依顺序打印列表中所有元素
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
方法一
print(names[0])
print(names[1])
print(names[2])
print(names[3])
print(names[4])
方法二
for i in names:
print(i)
结果:[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
8.调取一个列表元素的方法
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print(names[3])
结果:WuYing
八、统计列表中元素的个数
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
print(len(names))
结果:6
九、增加列表中元素的方法:append方法
score = []
print(score)
score.append(80)
print(score)
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.append(1705)
names.append('HZH')
print(names)
print(names.append('HZH')) # 无返回值,names.append
结果:
[]
[80]
[‘陈贤贤’, ‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’, 1705, ‘HZH’]
None
十、列表转换成字符串
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
a = "、".join(names)
print(a)
结果:陈贤贤、大财神、飞鱼、WuYing、阿阳、Black
十一、删除元素
1.按照元素删除
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names.remove('飞鱼')
print(names)
结果:[‘陈贤贤’, ‘大财神’, ‘WuYing’, ‘阿阳’, ‘Black’]
2.按照索引删除
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names = names.pop() # 不写索引就删除最后一个之外所有其他的
print(names)
‘’‘
结果:Black
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names = names.pop(2) # 保留第3个,删除所有其他的
print(names)
结果:飞鱼
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
names = names.clear() # 清除列表
print(names)
结果:None
names = ["陈贤贤", "大财神", "飞鱼", "WuYing", "阿阳", "Black"]
for i in range(len(names)): # 一个一个地删除
print(i)
del names[0]
print(names)
结果:
0
[‘大财神’, ‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
1
[‘飞鱼’, ‘WuYing’, ‘阿阳’, ‘Black’]
2
[‘WuYing’, ‘阿阳’, ‘Black’]
3
[‘阿阳’, ‘Black’]
4
[‘Black’]
5
[]