python学习列表字符串字典集合文件操作字符串编码与转换
Posted spring33410
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习列表字符串字典集合文件操作字符串编码与转换相关的知识,希望对你有一定的参考价值。
一、列表
1 names = "ZhangYang GuYun XiangPeng XuLiangchen" 2 names = ["ZhangYang", "GuYun", "XiangPeng", "ChengRongHua","XuLiangchen"] 3 names.append("LeiHaiDong") #在列表最后追加一个元素 4 names.insert(1,"ChengRongHua")#在列表中插入一个元素,insert后第一个参数是要插入的下表值,第二个参数是要插入的元素 5 names.insert(3,"XinZhiYu") 6 #names[2] = "XieDi" #替换列表下标为2的元素为XieDi 7 print(names) 8 #print(names[0],names[2]) #取列表值,第一个从0开始 9 #print(names[1:3])#切片 10 #print(names[-1])#切片取最后一个值 11 #print(names[-2:])#切片取最后两个值 12 #print(names[:3])#切片取前三个值 13 # delete 14 #names.remove("ChengRongHua") #按照查找元素删除 15 #del names[1] #删除下标为1的元素 16 #names.pop() #默认删除列表最后一个元素 17 #names.pop(1)#删除下标为1的元素与 del names[1] 效果相等 18 #print(names.index("GuYun")) #查找列表中GuYun所在的位置 19 #print(names[names.index("GuYun")])#查找列表中GuYun所在的位置,并将查找位置当列表值的下标 20 #print(names.count("ChengRongHua"))#统计列表中元素是ChengRongHua的数量 21 #names.clear()# 22 #names.reverse()#列表反转,最后一个元素与第一个元素对调,后续依次类推 23 #names.sort()#列表排序,排序规则按照ASCII排序 24 names2 = ["LiJian","LiLei"] 25 names.extend(names2) #将names2列表合并到names最后面 26 del names2 #删除names2整个列表
#列表copy 浅copy 和深copy 列表外层被完整复制,当原列表发生变化后,新复制的列表不变 import copy names = ["ZhangYang", "GuYun", "XiangPeng", "ChengRongHua","XuLiangchen"] names.insert(3,"XinZhiYu") #print(names) names2 = names.copy() print(names,‘ ‘,names2) names[3] = "向鹏" print("----------------") print(names,‘ ‘,names2) ‘‘‘ 执行结果 E:softwarepystudyvenvScriptspython.exe E:/software/pystudy/day2/names.py [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, ‘XinZhiYu‘, ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, ‘XinZhiYu‘, ‘ChengRongHua‘, ‘XuLiangchen‘] ---------------- [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, ‘向鹏‘, ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, ‘XinZhiYu‘, ‘ChengRongHua‘, ‘XuLiangchen‘] Process finished with exit code 0 ‘‘‘
#列表copy 浅copy 和深copy 列表外层被完整复制,但被复制的列表中的子列表发生改变时,新列表也会同时发生变化,这是由于子列表在内存中保存机制导致, #子列表打印时调用的是内存地址,并不是子列表本身元素值。
#当执行names.copy时,实际上子列表是copy的内存地址 import copy names = ["ZhangYang", "GuYun", "XiangPeng", ["alex", "Jack"], "ChengRongHua","XuLiangchen"] #print(names) names2 = names.copy() print(names, ‘ ‘, names2) names[2] = "向鹏" names[3][0] = "ALEX" print("----------------") print(names, ‘ ‘, names2) ‘‘‘ E:softwarepystudyvenvScriptspython.exe E:/software/pystudy/day2/names.py [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] ---------------- [‘ZhangYang‘, ‘GuYun‘, ‘向鹏‘, [‘ALEX‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘ALEX‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] Process finished with exit code 0 ‘‘‘
#列表copy 深copy 是完成创建一个相同的列表,完全独立的两个列表 import copy names = ["ZhangYang", "GuYun", "XiangPeng", ["alex", "Jack"], "ChengRongHua","XuLiangchen"] #print(names) names2 = copy.deepcopy(names) print(names, ‘ ‘, names2) names[2] = "向鹏" names[3][0] = "ALEX" print("----------------") print(names, ‘ ‘, names2) ‘‘‘ E:softwarepystudyvenvScriptspython.exe E:/software/pystudy/day2/names.py [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] ---------------- [‘ZhangYang‘, ‘GuYun‘, ‘向鹏‘, [‘ALEX‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] Process finished with exit code 0 ‘‘‘
#列表 循环 names = ["ZhangYang", "GuYun", "XiangPeng", ["alex", "Jack"], "ChengRongHua","XuLiangchen"] print(names ) print("------------") for i in names: print(i) print("------------") print(names[0:-1:2] )#切片步长取列表值,列表从第一个元素起,每间隔一个元素,打印一个元素 print(names[0:-1:2] )#与上面结果相同 ‘‘‘ E:softwarepystudyvenvScriptspython.exe E:/software/pystudy/day2/names.py [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] ------------E:softwarepystudyvenvScriptspython.exe E:/software/pystudy/day2/names.py [‘ZhangYang‘, ‘GuYun‘, ‘XiangPeng‘, [‘alex‘, ‘Jack‘], ‘ChengRongHua‘, ‘XuLiangchen‘] ------------ ZhangYang GuYun XiangPeng [‘alex‘, ‘Jack‘] ChengRongHua XuLiangchen ------------ [‘ZhangYang‘, ‘XiangPeng‘, ‘ChengRongHua‘] [‘ZhangYang‘, ‘XiangPeng‘, ‘ChengRongHua‘] Process finished with exit code 0 ‘‘‘
二、 元组:
元组其实和列表差不多,也是存一组数,只是它一旦创建,便无法修改,所以又叫只读列表。
语法:
names("alex", "jack", "eric")
它只有两个方法,一个count,一个index,完毕
#购物车程序 ‘‘‘ 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表 2、允许用户根据商品编号购买商品 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4、可随时退出,退出时,打印已购买商品和余额 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示 ‘‘‘ product_list = [ (‘Iphone‘, 5800,), (‘Mac Pro‘, 9800), (‘Bike‘, 800), (‘Watch‘, 10600), (‘Coffee‘, 31), (‘Alex Python‘, 120), ] shoping_list = [] #定义临时列表存放已购买的商品 salary = input("Input your salary:") if salary.isdigit(): #isdigit 判断整数 salary = int(salary) while True: for index, item in enumerate(product_list):#enumerate将列表下标取出来 print(index, item) user_choice = input("选择要购买的商品>>>:") if user_choice.isdigit():#isdigit判断是否是数字 user_choice = int(user_choice) if user_choice < len(product_list) and user_choice >= 0: p_item = product_list[user_choice] if p_item[1] <= salary: #工资够买选择的商品 shoping_list.append(p_item) salary -= p_item[1] print("Added %s into shopping cart, you current balance is 33[31;1m%s 33[0m" %(p_item,salary)) #打印字体为红色,31是红色,32是绿色 33 黄色 else: print("