0
点赞
收藏
分享

微信扫一扫

DAY4 字符串&列表(下)

1.列表如何去重(不能用set,不能用dict)

在pychram中使用ctrl+atl+l 代码好看一点

>>> li = [3,209,44,2,4,56,2,3,5,1,37,9,6,4,3,5,78,99,55,6,4]

# 怎么判断某个元素有没有重复?
li.count(8) > 1 :重复
想知道列表里每个元素有没有重复,就得所有元素count一次
# 怎么判断每个重复的值,要删除几次?
# 首先得找出所有重复的值
# 把所有重复的值,单独放一个列表

方法1:
duplicate_nums = []

for i in dulicate_nums:
for j in range(li.count(i)-1)
li.remove(i)
方法2:
duplicate_nums = [[1,3],[2,2]...]

---------------------------------------------------------------------------------

e.g.:
li = [3, 209, 44, 2, 4, 56, 2, 3, 5, 1, 37, 9, 6, 4, 3, 5, 78, 99, 55, 6, 4]
duplicate_nums = []

for i in li:
i_show_count = li.count(i) # 每个值出现了几次
if i_show_count > 1 and [i, i_show_count] not in duplicate_nums: # 代表该值是重复的
duplicate_nums.append([i, i_show_count])
# 不加and [i, i_show_count] not in duplicate_nums时duplicate_nums=[[3, 3], [2, 2], [4, 3], [2, 2], [3, 3], [5, 2], [6, 2], [4, 3], [3, 3], [5, 2], [6, 2], [4, 3]]
# 有多次重复值需要删除
print(duplicate_nums)
# [[3, 3], [2, 2], [4, 3], [5, 2], [6, 2]]

for item in duplicate_nums:
# duplicate_n = item[0]
# duplicate_times = item[1] # 重复次数
dumplicate_n,duplicate_times = item
for j in range(duplicate_times - 1): # 为何减1 去重而不是把所有重复值删除,保留一个重复值
li.remove(duplicate_n) # 一次只能删除一个值
print(li)
# [209, 44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4]

2.找到列表中第2大的值(不能用sort)

求最大值
li = [209, 44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4]
max_n = li[0]
for i in li:
if i > max_n:
max_n = i
print(max_n)

冒泡排序法求最大值
li = [209, 44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4]
for index in range(len(li) - 1):
i = li[index] # 拿到当前索引的值
if i > li[index + 1]:
li[index] = li[index + 1]
li[index + 1] = i
print(li)

求第2大的值
利用冒泡排序法将最大值切换至最后面

li = [209, 44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4]
for n in range(2):
for index in range(len(li) - 1):
i = li[index] # 拿到当前索引的值
if i > li[index + 1]:
li[index] = li[index + 1]
li[index + 1] = i
print(li)
print(li[-2])

3.判断一个列表是不是另一个列表的子列表

li = [209, 44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4]
li2 = [44, 56, 37, 9, 223]

is_sub_list = True
for i in li2:
if i not in li:
is_sub_list = False
if is_sub_list:
print('li2是li的子列表')
else:
print('li2不是li的子列表')

4.求出列表中,离最大值和最小值的平均值最接近的值

li = [44, 56, 2, 1, 37, 9, 3, 5, 78, 99, 55, 6, 4, 209]
max_n = li[0]
min_n = li[1]

for i in li:
if i > max_n: max_n = i
if i < min_n: min_n = i
avg_n = (max_n + min_n) / 2
print(max_n, min_n, avg_n)

closest_n = li[0] # 假设是最近的值
for n in li:
if abs(avg_n - n) < abs(avg_n - closest_n): # abs()绝对值
closest_n = n
print('找到更进的了:', closest_n)

print(closest_n)

二、综合小实战

1.双色球选购程序(共33个红球选6个,共16个蓝球选1个)

red_balls = []
blue_balls = []
count = 0
while count < 6:
choice = input(f"输入第{count+1}个红球>:").strip()
if not choice.isdigit():
print("不合法")
continue
choice = int(choice)
if 0 < choice <= 33 and choice not in red_balls:
red_balls.append(choice)
count += 1

count = 0
while count < 1:
choice = input(f"输入第{count+1}个蓝球>:").strip()
if not choice.isdigit():
print("不合法")
continue
choice = int(choice)
if 0 < choice <= 16 and choice not in blue_balls:
blue_balls.append(choice)
count += 1

print(red_balls,blue_balls)

red_balls = []
blue_balls = []

li = [
[6, 33, "红球", red_balls],
[1, 16, "蓝球", blue_balls]
]

for item in li:
print(f"开始选择{item[2]}".center(50, '-'))
count = 0
while count < item[0]:
choice = input(f"输入第{count + 1}{item[2]}>:").strip()
if not choice.isdigit():
print("不合法")
continue
choice = int(choice)
if 0 < choice <= item[1] and choice not in item[3]:
item[3].append(choice)
count += 1
print(red_balls,blue_balls)

作业:

购物车程序:

需求:

       1.我和姑娘去逛街,程序实现打印商品列表,用户可通过商品编号来选购商品,允许不断的买商品

       2.程序启动时,让用户先输入自己的工资,总购物的商品价格不得超过工资

       3.用户随时可退出程序,退出时,打印,分别买了哪些商品及数量(同一商品只显示一个),及余额

规则:不能使用dict、set


商品列表 [[序号,商品名称,单价],[序号,商品名称,单价]]

>购物车列表[[商品名称,单价,数量],[商品名称,单价,数量]] --> 用户添加的物品列表

#!/usr/bin/env python
# -*- coding:UTF-8 -*-

true_and_false = True
# 输入一个工资金额salary
while true_and_false:
salary = input("请输入您的工资:").strip()
if salary.isdigit():
true_and_false = False

commodity_li = [[1,"iphone",9600],[2,"mac",15000],[3,"ipad",6000]]

print()
print('序号'.center(3),'产品名称'.ljust(4),'价格'.center(4))

num_list = []
commodity_name_list = []

for i in commodity_li:
print(str(i[0]).center(3),i[1].ljust(6),str(i[2]).center(5))
num_list.append(i[0])
commodity_name_list.append(i[1])

# print(num_list)
# print(commodity_name_list)

true_and_false = True
choice_commodity = []
end_choice_commodity_list = []

while true_and_false:
num_or_commodity_name = input("请输入产品名称或序号添加商品,按Y确认支付,按N退出选购。").strip().lower()
if num_or_commodity_name == 'y':
if len(choice_commodity) == 0:
print("您没有选购商品,欢迎下次光临")
break
else:
for commodity in choice_commodity:
commodity_count = choice_commodity.count(commodity)
if [commodity[1],commodity_count,commodity[2]*commodity_count] not in end_choice_commodity_list:
end_choice_commodity_list.append([commodity[1],commodity_count,commodity[2]*commodity_count])

# print(end_choice_commodity_list)
# [['iphone', 4, 38400], ['mac', 3, 45000], ['ipad', 2, 12000]]

total_price = 0
total_num = 0
for i in end_choice_commodity_list:
total_price += i[2]
total_num += i[1]

if total_price <= int(salary):
print('产品名称'.ljust(5), '数量'.center(2), '总价'.center(4))
for i in end_choice_commodity_list:
print(i[0].ljust(7), str(i[1]).center(4), str(i[2]).center(5))
print(f"您选购了{total_num}件商品,总价为:{total_price}")
print(f"您的余额为:{int(salary)-total_price}")
else:
print(f"您选购的商品总价比您的工资高,无法购买,已为您清空购物车")
choice_commodity = []
end_choice_commodity_list = []
break
elif num_or_commodity_name == 'n':
print("欢迎下次光临")
break
elif num_or_commodity_name.isdigit() and int(num_or_commodity_name) in num_list:
choice_commodity.append(commodity_li[num_list.index(int(num_or_commodity_name))])
print(f"您已选购1件商品{commodity_li[num_list.index(int(num_or_commodity_name))][1]}")
continue
elif num_or_commodity_name in commodity_name_list:
choice_commodity.append(commodity_li[commodity_name_list.index(num_or_commodity_name)])
print(f"您已选购1件商品{commodity_li[commodity_name_list.index(num_or_commodity_name)][1]}")
continue
else:
print('没有此商品,请重新选择')




举报

相关推荐

字符串——列表

-day4

Day4

day4 QT

Qt day4

QT DAY4

hadoop——day4()

0 条评论