第一个程序 三级菜单-ThreeLevelMenu
# encoding: utf-8 ''' @author: ccq @file: ThreeLevelMenu.py @time: 2019/5/24 18:00 ''' # 三级菜单 data = { '北京': { "朝阳": { "望京": ["奔驰", "陌陌"], }, "昌平": { "沙河": ["oldboy", "test"], }, }, '山东': { "青岛": {}, "济南": {}, }, '广东': { "东莞": {}, "佛山": {}, }, } # 判断符 flag = False while not flag: for i1 in data: print(i1) choice = input("选择进入1>>>:") if choice in data: while not flag: for i2 in data[choice]: print("\t", i2) choice2 = input("选择进入2>>>:") if choice2 in data[choice]: while not flag: for i3 in data[choice][choice2]: print("\t", i3) choice3 = input("选择进入3>>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print(i4) choice4 = input("最后一层,按b键返回>>>4:") if choice4 == "b": pass # 占位符 elif choice4 == "q": flag = True if choice3 == "b": break elif choice3 == "q": flag = True if choice2 == "b": break elif choice2 == "q": flag = True#用最基础的语法和字典实现一个三级菜单栏,以北京区为例,所以下面两个都没有补全。
#这里就表现出了python比较恶心的一点:python非常注重格式,不然它的嵌套就会有问题。但是代码一多,在没有编辑器的情况下,就显得很难去阅读。
===========================================分割线================================================
第二个程序
列表去除重复-RemoveListDuplication
# encoding: utf-8 ''' @author: ccq @file: RemoveListDuplication.py @time: 2019/5/27 10:27 ''' # 去除列表重复 list1 = [1, 2, 3, 4, 5, 2, 3, 1, 5, 6, 7, 8, 7, 4, 7, 8, 9, 9, 1, 2] list2 = [] for i in list1: if i in list2: continue else: list2.append(i) print(list2)#这个代码其实一点也不复杂,甚至可以说是基操。但是我在写的时候就遇到了一点问题。这里分享一下问题:在java或者别的语言中,想要实现列表去除重复也不难,但是和python的操作不同。我刚开始写的时候,一度写出了如下代码:
for i in list1:
list2[i]=i
#但是这显然是不对的。以为i这个变量每一次循环后都代表一个list1中的值。但是我确实又需要下标,那怎么办呢?于是又写了以下代码:
for index,i in enumerate(list1):
list2[index]=i
#这里做个tip:enumerate方法对前面参数的顺序十分重要,如果你是i在前,index在后,就像这样:
for i,index in enumerate(list1):
那么此时i才是下标,index是list1中的元素!千万别搞错!
#继续回到刚才的问题。这样写显然也是不对的,在写python的时候,那就应该变成一个python人,而不是java人或者c++人或者别的什么。
#当然,读懂报错信息也是特别重要的一环。写上面的代码的时候报的错误是:list assignment index out of range 翻译成中文就是:列表分配索引超出范围
#因为list2是个空列表,所以list2[index]显然是会出现越界的问题。
#记录一下自己犯的错误,避免以后再犯。
===========================================分割线================================================
第三个程序
集合操作-SetOperations
# encoding: utf-8 ''' @author: ccq @file: SetOperations.py @time: 2019/5/27 11:03 ''' # 集合操作 list1 = [1, 1, 4, 5, 5, 6, 3, 2, 2] list1 = set(list1) print(list1, type(list1)) list2 = set([4, 7, 6, 8, 9, 8]) print(list2, type(list2)) # 交集 print("交集") print(list1.intersection(list2)) print(list1 & list2) # 并集 print("并集") print(list1.union(list2)) print(list1 | list2) # 差集 print("差集") print(list1.difference(list2)) print(list1 - list2) # 子集 print("子集") print(list1.issubset(list2)) # 父集 print("父集") print(list1.issuperset(list2)) # 对称差集 print("对称差集") print(list1.symmetric_difference(list2)) print(list1 ^ list2) # 判断两个集合是否包含相同的元素,不包含返回true print("判断两个集合是否包含相同的元素,不包含返回true") print(list1.isdisjoint(list2)) # 添加 list3 = set([1, 2, 3, 4, 5]) # 添加一项 list3.add(6) print(list3) # 添加多项 list3.update([7, 8, 9]) print(list3) # 随机删除项 print(list3.pop()) # 删除指定项 list3.remove(2) list3.discard(3) print(list3)#集合是无序的!
#都是一些常用的集合操作
===========================================分割线================================================
第四个程序
文件操作-FileOperations
# encoding: utf-8 ''' @author: ccq @file: FileOperations.py @time: 2019/5/27 14:35 ''' # 文件操作 # 文件句柄 file1 = open("yesterdayoncemore", "r", encoding="utf-8") # 读文件 data = file1.read() print(data) file1.close() # 写文件 file2 = open("yesterday", "w", encoding="utf-8") text = "ttttt\nnnnnnnnnn" file2.write(text) file2.close() # 写文件 file3 = open("yesterdayonce", "a", encoding="utf-8") file3.write("\n最后一颗子弹留给我") file3.close() # 读5-10行的内容 file4 = open("yesterdayoncemore", "r", encoding="utf-8") print("\nfile4:") for i in range(10): if i < 4: file4.readline() else: print(file4.readline()) # 读5-10行的内容2,如果文件过大会把内存撑满,low print("fil4,secondway:") for index, i in enumerate(file4.readlines()): if index < 4: continue print(i.strip()) if index > 10: break file4.close() # 读5-10行的内容,比较好的方法 file5 = open("yesterdayonce", "r", encoding="utf-8") print("\nfile5:") count = 0 # 这里的ifle5变成了迭代器 for i in file5: if count < 5: count += 1 continue if count > 10: break print("\033[31;1m %s \033[0m" % (i)) count += 1 file5.close()#对文件进行操作时,一定要注意格式:file = open("yesterdayoncemore", "r", encoding="utf-8")
#第一个表示文件名,第二个表示模式,第三个表示字符编码。
#r模式下只能读,不能写;w模式下只能写不能读;a模式下也是只能写不能读。
#w模式和a模式的区别:w模式每次重新创建一个名字相同的新文件覆盖原文件;而a模式只在末尾追加。
#读5-10行的内容2,这样的写法是一种不太好的写法,因为循环时,要预先把所有的东西都放到内存里来,如果文件过大,则内存会被撑满。
#对文件进行操作的时候,记得操作完要关闭。
===========================================分割线================================================
今天到此为止!明天继续努力!
