names = ["zhangSan","liSi","wangWu","xinHua"]
print(names) #['zhangSan', 'liSi', 'wangWu', 'xinHua']
print(names[0]) #zhangSan
print(names[0],names[2]) #zhangSan wangWu
print(names[1:3]) #['liSi', 'wangWu'] 切片 起始位置包括,结束位置不包括,顾头不顾尾
print(names[-1]) #xinHua -1取最后一个
#想取后两个值
print(names[-1:-3]) #[] 取不出来
print(names[-3:-1]) #['liSi', 'wangWu']
print(names[-2:-1]) #['wangWu']
print(names[-2:]) #['wangWu', 'xinHua']
#追加变量
names = ["zhangSan","liSi","wangWu","xinHua"]
names.append("ziDian")
print(names)
#插入变量
names = ["zhangSan","liSi","wangWu","xinHua"]
names.append("ziDian")
names.insert(1,"xiaoMing") #1是下标,插入到哪里写哪下标
print(names)
#不能批量插入,只能一个一个来
#改变量
names[1] = xiaoHong #提对应修改变量的下标
#两种删除方法
del names[1]
names.remove("xiaoHong")
#特殊删除
names.pop() #默认不输入下标的情况下删除最后一个,如果输入了下标删除对应下标的变量
#索引
print(names.index("xiaoHong")) #会返回它对应的下标
print(names[names.index("xiaoHong")]) #使用names中括号,打印的不再是下标值,打印出的是名字
#统计列表内重名
names = ["zhangSan","liSi","wangWu","xinHua"]
names.append("liSi")
print(names) #['zhangSan', 'liSi', 'wangWu', 'xinHua', 'liSi']
print(names.count("liSi")) #2
#清空
names.clear()
#反转
names.reverse()
#排序
names.sort() #特殊字符>数字>大写>小写
#合并
names2=[1,2,3,4]
names.extend(names2)
print(names,names2) #['zhangSan', 'liSi', 'wangWu', 'xinHua', 1, 2, 3, 4] [1, 2, 3, 4]
#复制
names2 = names.copy()
print(names)
print(names2)
names = ["zhangSan","liSi","wangWu","xinHua"]
names2 = names.copy()
names[1] = "李四"
print(names) #['zhangSan', '李四',"wangWu",'xinHua']
print(names2) #['zhangSan', 'liSi', 'wangWu', 'xinHua']
#列表内包含子列表
names = ["zhangSan","liSi","wangWu",["ziDian","xiaoMing"],"xinHua"] #["ziDian","xiaoMing"]这个子列表是names里的一个元素
#浅copy
names = ["zhangSan","liSi","wangWu",["ziDian","xiaoMing"],"xinHua"]
names2 = names.copy()
print(names) #['zhangSan', 'liSi', 'wangWu', ['ziDian', 'xiaoMing'], 'xinHua']
print(names2) #['zhangSan', 'liSi', 'wangWu', ['ziDian', 'xiaoMing'], 'xinHua']
names[1] = "李四"
names[3][0] = "字典"
print(names) #['zhangSan', '李四', 'wangWu', ['字典', 'xiaoMing'], 'xinHua']
print(names2) #['zhangSan', 'liSi', 'wangWu', ['字典', 'xiaoMing'], 'xinHua']
#copy不是完全克隆,是浅copy,只copy一层,机制是内存这么设定的,copy的是地址
#深copy
import copy
names = ["zhangSan","liSi","wangWu",["ziDian","xiaoMing"],"xinHua"]
names2 = copy.deepcopy(names)
print(names)
print(names2)
names[1] = "李四"
names[3][0] = "字典"
print(names) #['zhangSan', '李四', 'wangWu', ['字典', 'xiaoMing'], 'xinHua']
print(names2) #['zhangSan', 'liSi', 'wangWu', ['ziDian', 'xiaoMing'], 'xinHua']
补充:浅copy
import copy
person=['name'['a',100]]
#三种浅copy
p1=copy.copy(person #引用
p2=person[:] #完全切片
p3=list(person) #工厂函数list
import copy
person=['name'['saving',100]]
p1=person[:]
p2=person[:]
p1[0]='a'
p2[0]='b'
print(p1) #['a', ['saving', 100]]
prent(p2) #['b', ['saving', 100]]
联合账号
import copy
person=['name',['saving',100]]
p1=person[:]
p2=person[:]
p1[0]='a'
p2[0]='b'
p1[1][1]=50
print(p1) #['a', ['saving', 50]]
print(p2) #['b', ['saving', 50]]
元组跟列表差不多,也是存一组数,只是它一旦创建,便不能再修改,所以又叫只读列表
它只有两个办法,一个是count,一个是index
语法:name = ("a","b","c")
程序练习
请闭眼写出以下程序。
程序:购物车程序
需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
#我写的时候也是没思路,大王说得对,没思路,不会写就是代码写得少,无从下手
product_list=[ ('Iphone',5800), ('Mac Pro',9800), ('Bick ',800), ('Watch',10600), ('Coffee',31), ('book',120) ] shopping_list = [] salary = input("Input your salary:") #输入工资 if salary.isdigit(): #判断工资是不是数字 salary = int(salary) #如果是数字给它int ''' while True: for item in product_list: print(product_list.index(item),item) #通过下标找 break ''' while True: #进入一个死循环 for index,item in enumerate(product_list): print(index,item) #打印商品列表 user_chois = input("选择要买什么? >>>:") #让用户选择买什么东西 if user_chois.isdigit(): #判断用户输入必须是数字类型 user_chois = int(user_chois) if user_chois < len(product_list) and user_chois >= 0: #判断一下下标,避免出错 p_item = product_list[user_chois] #通过下标把商品取出来 if p_item[1] <= salary: #判断是否买得起 shopping_list.append(p_item) #添加到之前创建的空列表里 salary -= p_item[1] #扣钱 print("Added %s into shopping cart,your current balane is 33[31;1m%s 33[0m"%(p_item,salary)) else: print(" 33[41;1m你的余额只剩%s了 33[0m"%salary) else: print("product code [%s] is not exist"%user_chois) #输入的商品序号不存在 elif user_chois == 'q':#输入q退出 print('---shopping list---') for p in shopping_list: print(p) #打印购买的商品 print("Your current balance:",salary) exit() else: print("invalid option")
'''
enumerate作用
a=[1,2,3]
for i in enumerate(a):
print(i)
输出结果为:
(0, 1)
(1, 2)
(2, 3)
enumerate作用是把列表的下标取出来
'''