一、列表、元组操作
列表是我们以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。
定义列表
names=['lv','shi','hai','ll']
结果:通过下标访问列表中的元素,下标从0开始计数
print(names[1])
切片:取多个元素
names=['lv','shi','hai','ll']
print(names[1:3]) #取下标1至下标3之间的数字,包括1但不包括3
print(names[-1]) #取最后一个值
print(names[-2:]) #取最后两个值
print(names[:3])#取前三个值
步长切片:
names=['1lv','#shi','Hai',['hh','kk'],'ll','fadfa']
print(names[0:-1:2])#隔两个取值
print(names[::2]) #0和-1都可以省略
追加:
names=['lv','shi','hai','ll']
names.append('ls')
插入:
names=['lv','shi','hai','ll']
names.insert(2,'li') #数字代表下标,不能批量插入
修改:
names=['lv','shi','hai','ll']
names[2]='liu' #把li改为liu,
print(names)
删除:
方法一:
names=['lv','shi','hai','ll']
names.remove('lv')#删除lv
print(names)
方法二:
names=['lv','shi','hai','ll']
del names[0] #删除lv,方法二
print(names)
方法三:
names=['lv','shi','hai','ll']
names.pop() #默认不输入下标,删除最后一个值,输入下标后跟del用法一样
print(names)
获取下标:
names=['lv','shi','hai','ll']
print(names)
print(names.index('shi'))
names=['lv','shi','hai','ll']
print(names)
print(names[names.index('shi')])
统计:
names=['lv','shi','hai','ll']print(names.count('lv'))
print(names)
反转:
names=['lv','shi','hai','ll']
print(names)
names.reverse()
print(names)
排序:
names=['lv','shi','hai','ll']
print(names)
names.sort()
print(names)
扩展:
names=['lv','shi','hai','ll']
print(names)
names2=[1,2,3,4]
names.extend(names2)
print(names,names2)
拷贝:
names=['1lv','#shi','Hai','ll','fadfa']
names2=names.copy()
print(names)
print(names2)
浅copy:
names=['1lv','#shi','Hai',['hh','kk'],'ll','fadfa']
names2=names.copy()
names[2]='海'
names[3][0]='HH'
print(names)
print(names2)
import copy
names=['1lv','#shi','Hai',['hh','kk'],'ll','fadfa']
names2=copy.copy(names)#浅复制
names[2]='海'
names[3][0]='HH'#浅复制
print(names)
print(names2)
深copy:
import copy
names=['1lv','#shi','Hai',['hh','kk'],'ll','fadfa']
names2=copy.deepcopy(names)#深copy
names[2]='海'
names[3][0]='HH'
print(names)
print(names2)
列表循环:
names=['1lv','#shi','Hai',['hh','kk'],'ll','fadfa']
for i in names:
print(i)
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
语法
1
|
names = ( "alex" , "jack" , "eric" ) |
它只有2个方法,一个是count,一个是index,完毕。
names = ('lv','shi')
print(names.count('lv'))
names = ('lv','shi')
print(names.index('lv'))
程序练习
程序:购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
源代码如下:
product_list = [('Iphone',5800),('Mac pro',9800),('Bike',800),('Watch',1000),('Book',58)]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit(): #判断工资是否为数字 isdigtit判断是否为数字
salary = int(salary)
while True:
# for item in product_list:
# print(product_list.index(item),item) #通过获取下标的方式来获取商品编号
for index,item in enumerate(product_list): #通过获取下标的方式来获取商品编号 enumerate表示取出列表的下标
print(index,item)
user_choice = input("请选择要买的物品:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >=0: #len 表示判断列表长度
p_item = product_list[user_choice] #获取产品
if p_item[1] <= salary: #买得起 p_item[1]表示商品价格
shopping_list.append(p_item) #添加到购物车
salary -= p_item[1] #扣钱
print("增加 %s 到购物车,您的余额是: