Python基础,day2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础,day2相关的知识,希望对你有一定的参考价值。
本节内容
- 列表、元组操作
- 字符串操作
- 字典操作
- 集合操作
- 文件操作
- 字符编码与转码
1. 列表、元组操作
列表是我们以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
name = ["Martin","Annie","JMA","Martin","Ly"]
通过下标访问列表中的元素,下标从0开始计数
1 >>> name[0] 2 ‘Martin‘ 3 >>> name[4] 4 ‘Ly‘ 5 >>> name[-1] #可以倒着取 6 ‘Ly‘
切片:取多个元素
>>> name = ["Martin","Annie","JMA","Martin","Ly"] >>> name[1:4] #取下标1至下标4之间的数字,包括1,不包括4 [‘Annie‘, ‘JMA‘, ‘Martin‘] >>> name[0:3] #取下标0至下标3间的数字,包括0,不包括3 [‘Martin‘, ‘Annie‘, ‘JMA‘] >>> name[:3] #如果是从头开始取,0可以忽略,跟上句效果一样 [‘Martin‘, ‘Annie‘, ‘JMA‘] >>> name[3:-1] #取下标3至-1的值,不包括-1 [‘Martin‘] >>> name[3:] #如果想取最后一个,必须不能写-1,只能这么写 [‘Martin‘, ‘Ly‘] >>> name[0::2] #后面的2是代表,每隔一个元素,就取一个 [‘Martin‘, ‘JMA‘, ‘Ly‘] >>> name[::2] #和上面的效果一样 [‘Martin‘, ‘JMA‘, ‘Ly‘]
追加
>>> name.append("HZY") >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘]
插入
>>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘] >>> name.insert(1,"HZY") >>> name [‘Martin‘, ‘HZY‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘]
修改
[‘Martin‘, ‘HZY‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘] >>> name[1] = "acer" >>> name [‘Martin‘, ‘acer‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘]
删除
>>> name [‘Martin‘, ‘acer‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘] >>> del name[1] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘, ‘HZY‘] >>> name.remove("HZY") #删除指定元素 >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Ly‘] >>> name.pop() #删除列表最后一个值 ‘Ly‘ >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘]
扩展
>>> name2 = [1,2,3,4] >>> name.extend(name2) >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, ‘Martin‘, 1, 2, 3, 4]
拷贝
浅copy,列表的copy >>> name = ["Martin", "Annie", "JMA", ["ABC", "test"], "Martin"] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘ABC‘, ‘test‘], ‘Martin‘] >>> name2 = name.copy() >>> name2 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘ABC‘, ‘test‘], ‘Martin‘] >>> name[3][0] ‘ABC‘ >>> name[3][0] = "abc" >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] >>> name2 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] 浅copy,copy模块的copy >>> import copy >>> name3 = copy.copy(name) >>> name3 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] >>> name[3][0] = "123" >>> name3 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘123‘, ‘test‘], ‘Martin‘] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘123‘, ‘test‘], ‘Martin‘] deepcopy,copy模块的deepcopy,一般别用 >>> import copy >>> name4 = copy.deepcopy(name) >>> name4 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘123‘, ‘test‘], ‘Martin‘] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘123‘, ‘test‘], ‘Martin‘] >>> name[3][0] = "abc" >>> name4 [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘123‘, ‘test‘], ‘Martin‘] >>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘]
统计
>>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] >>> name.count("Martin") 2
排序&翻转
>>> name [‘Martin‘, ‘Annie‘, ‘JMA‘, [‘abc‘, ‘test‘], ‘Martin‘] >>> name.sort() #排序 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘<‘ not supported between instances of ‘list‘ and ‘str‘ #3.0里不同数据类型不能放在一起排序了 >>> name[3] = "test" >>> name [‘Annie‘, ‘JMA‘, ‘Martin‘, ‘test‘, ‘Martin‘] >>> name.sort() >>> name [‘Annie‘, ‘JMA‘, ‘Martin‘, ‘Martin‘, ‘test‘] >>> name.reverse() #反转 >>> name [‘test‘, ‘Martin‘, ‘Martin‘, ‘JMA‘, ‘Annie‘]
获取下标
>>> name [‘test‘, ‘Martin‘, ‘Martin‘, ‘JMA‘, ‘Annie‘] >>> name.index("JMA") 3
清空列表
name.clear() #清空列表
列表循环
for i in name: for j in i: print(j) print(i)
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
语法:
>>> name = ("Martin", "JMA", "Ly") >>> name (‘Martin‘, ‘JMA‘, ‘Ly‘)
它只有2个方法,一个是count,一个是index
2. 字符串操作
特性:不可修改
3. 字典操作
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
语法:
info = { "stud001": "xiaoming", "stud002": "xiaohong", "stud003": "xiaodong", "stud004": "xiaomei" }
字典的特性:
- dict是无序的
- key必须是唯一的
增加
info["stud005"] = "xiao" print(info.setdefault("stud004","martin")) #key存在时打印对应value print(info.setdefault("stud005","martin")) #key不存在时增加
修改
info["stud001"] = "xiao" info["stud001"] += ",xiao" {‘stud001‘: ‘xiaoming,xiao‘, ‘stud002‘: ‘xiaohong‘, ‘stud003‘: ‘xiaodong‘, ‘stud004‘: ‘xiaomei‘}
删除
info.pop("stud001") #指定删除 info.popitem() #随机删除 del info["stud002"] #python的删除方法
查找
"stud003" in info #标准用法,判断key是否在字典里,存在未Ture,不存在为False True print(info.get("stud001")) #这个不会,不存在会返回none print(info["stud001"]) #该方法当key不存在时会报错
其他操作
print(info.keys()) #获取字典的key列表 print(info.values()) #获取字典的value列表 print(info.items()) #将字典转换成列表 dict_items([(‘stud001‘, ‘xiaoming‘), (‘stud002‘, ‘xiaohong‘), (‘stud003‘, ‘xiaodong‘), (‘stud004‘, ‘xiaomei‘)])
字典循环
方法1: for key in info: print(key,info[key]) 方法二: for k,v in info.items(): #会先把dict转成list,数据量大时莫用 print(k,v)
字典更新合并
>>> info {‘stud001‘: ‘xiaoming‘, ‘stud002‘: ‘xiaohong‘, ‘stud003‘: ‘xiaodong‘, ‘stud004‘: ‘xiaomei‘} >>> b {1: 2, 3: 4} >>> info.update(b) >>> info {‘stud001‘: ‘xiaoming‘, ‘stud002‘: ‘xiaohong‘, ‘stud003‘: ‘xiaodong‘, ‘stud004‘: ‘xiaomei‘, 1: 2, 3: 4}
多级字典嵌套及操作
menu = { "jiangxi": { "nanchang": { "xinjianxian": ["ncu", "bank"], "nanchangxian": ["caijingdaxue", "jishuxueyuan"] }, "jiujiang": { "xiushuixian": "修水县地图", "jiujiangxian": "九江县地图" } }, "guangdong": { "shenzhen": { "longhuaqu": "龙华区地图", "luohuqu": "罗湖区地图" } } } menu["jiangxi"]["nanchang"]["xinjianxian"][0] += " univercity" print(menu["jiangxi"]["nanchang"]["xinjianxian"][0]) ncu univercity
程序练习
程序: 三级菜单
要求:
- 打印省、市、县三级菜单
- 可返回上一级
- 可随时退出程序
exit_flag = False while not exit_flag: for i in menu: print(i) choice = input("1>>:") if choice in menu: while not exit_flag: for i2 in menu[choice]: print("\t", i2) choice2 = input("2>>:") if choice2 in menu[choice]: while not exit_flag: for i3 in menu[choice][choice2]: print("\t\t", i3) choice3 = input("3>>:") if choice3 in menu[choice][choice2]: for i4 in menu[choice][choice2][choice3]: print("\t\t\t",i4) choice4 = input("4按b返回上层>>:") if choice4 == "b": pass elif choice4 == "q": exit_flag = True if choice3 == "b": break elif choice3 == "q": exit_flag = True if choice2 == "b": break elif choice2 == "q": exit_flag = True
以上是关于Python基础,day2的主要内容,如果未能解决你的问题,请参考以下文章