标签:== while循环 基础 添加 color 学习 保存 改变 append
1、在for循环中,可用else,当循环结束以后才执行else后面的语句,但如果for循环被break打断的话是不会执行else的,相当于与while搭配的else。
1 # 在while循环中,else是当while条件不满足的时候执行的 2 # 如果while循环是由break打断的,则不执行else 3 index = 1 4 while index < 11: 5 index = index + 1 6 if index == 5: 7 # reak 8 pass 9 print(index) 10 else: 11 print("fuck you man") 12 print("son of bitch") 13 14 for i in range(1,11): 15 print(i) 16 if i == 3: 17 break # 也可以使用break和continue 18 else: # for循环后面也可以有else 19 print("this is normal") 20 print("there is nothing here")
1、 .join() 把列表变成字符串
2、 split() 把字符串转换成列表
1 lst = ["alex","allen","tony","tom"] 2 s = "#".join(lst) 3 print(s) 4 5 s = "你今天学习python了么?" 6 s = "_".join(s) 7 print(s) 8 9 s = "你今天准备什么时候睡觉" 10 s1 = "_".join(s) 11 print(s1) 12 lst = s1.split("_") 13 print(lst)
1、列表在循环的时候如果执行了删除操作,索引和长度会有变化,这种删除是不安全的,先把要删除的内容放在一个新列表中,然后循环这个新列表,删除老列表
2、dict在循环过程中是不允许删除的,要把要删除的键放在列表中,循环列表删除字典
1 lst = ["倚天屠龙记","诛仙","看见","追风筝的人"] 2 # 正确 3 for i in range(0,len(lst)): 4 lst.pop() 5 print(lst) 6 # 把要删除的东西保存在一个新列表中 7 new_lst = [] 8 for el in lst: 9 new_lst.append(el) 10 # 循环新列表,删除老列表 11 for e in new_lst: 12 lst.remove(e) 13 print(lst)
四、字典的相关操作
字典有一个静态方法fromkeys()
1 # fromkeys() 2 # 把第一个参数中的每一项拿出来和第二个参数组成一个键值对 3 # 生成出来的键指向的value是同一个对象,改变其中一个的时候,另一个也会跟着改变 4 dic = dict.fromkeys(["jay","jj"],["周杰伦","林俊杰"]) 5 print(dic) 6 dic["jay"].append("蔡依林") 7 print(dic) 8 dic = {} 9 dic.fromkeys(["葫芦娃","蛇精"],["冯提莫","陈一发"]) 10 print(dic) # 什么都不打印,因为fromkeys是个静态方法 11 # 字典在循环迭代的过程中不能删除,会发生错误 12 dic = {"alex":15,"allen":23,"kitty":18} 13 for item in dic: # 直接循环迭代的时候拿到的是key 14 if item == "alex": 15 dic.pop(item) # RuntimeError: dictionary changed size during iteration 16 17 dic = {"alex":15,"allen":23,"kitty":18} 18 lst = ["alex","allen"] 19 for el in lst: # 循环list 20 dic.pop(el) # 删除的是dict中的元素 21 print(dic)
lst = ["发图哥","谁困","就轰炸谁"] print(type(lst)) tu1 = tuple(lst) print(type(tu1)) lst1 = list(tu1) print(type(lst1)) # 能转换成False的数据类型有哪些? # 0, "", [], {}, (), None, set(), False
六、集合
set 就是存储的key set集合不会重复,无序set中的元素是可哈希的,但set本身是不可哈希的set的作用最重要的就是 去掉重复项
set 添加项用 add() update()迭代的去增加,括号内的数据类型必须是可迭代的
交集/并集/差集frozenset 此种集合是不可变的 也就是可哈希的
1 # dic = {"朱元璋":"明太祖", "朱棣":"明成祖", "朱元璋":"朱重八"} 2 # dic[‘朱元璋‘] = "和尚" 3 # print(dic) 4 # # set就是存储的key, set集合不会重复, 无序 5 # s = {"朱元璋", "朱元璋", "朱棣"} 6 # print(s) 7 8 # s = {"alex", 1, True, {"你好"}} 9 # s = {"马虎疼", [1,4,7]} # 不合法. [] 列表是不可哈希的 10 # s = {"呵呵", "哈哈", "吼吼", (1, 4, 7)} 11 # print(s) 12 13 # 去重复 14 # lst = [1,4,7,1,4,5,6,7,8,2,1,3,9] 15 # s = set(lst) 16 # lst = list(s) 17 # print(lst) 18 19 # s = set({}) 20 # s.add("王祖蓝") # 添加元素 21 # s.add("王祖贤") 22 # s.add("王祖蓝") 23 # print(s) 24 25 s = {"李谷一", "周润发", "邓丽君", "黄家驹", "张国荣"} 26 # s.update("中岛美嘉") # 迭代更新 27 # print(s) 28 29 # s.update(["中岛美雪", "很牛B"]) 30 # print(s) 31 # s = {"李谷一", "周润发", "邓丽君", "黄家驹", "张国荣"} 32 # item = s.pop() # 随机删除 33 # print(item) 34 # s.clear() 35 # print(s) 36 # for el in s: 37 # print(el) 38 39 # s1 = {"刘能", "赵四", "皮长山"} 40 # s2 = {"刘科长", "冯乡长", "皮长山"} 41 42 # 求交集 43 # print(s1 & s2) 44 # print(s1.intersection(s2)) # 交集 45 46 # 并集 47 # print(s1 | s2) 48 # print(s1.union(s2)) # 并集 49 50 # s1 = {"刘能", "赵四", "皮长山"} 51 # s2 = {"刘科长", "冯乡长", "皮长山"} 52 # 差集, 把自己本身中和对方不相关的内容留下 53 # print(s2 - s1) 54 # print(s2.difference(s1)) 55 56 # s = frozenset({1,5,7}) 57 # # print(s) 58 # # # for el in s: 59 # # # print(el) 60 # dic = {s: "周杰伦"} # 验证. frozen是可哈希的. 不可变 61 # print(dic)
七、深浅拷贝问题
在python中存在深浅拷贝问题,具体见代码:
1 # 深浅拷贝 2 lst1 = ["紫衫龙王","白眉鹰王","青翼蝠王","金毛狮王"] 3 lst2 = lst1 4 print(id(lst1),id(lst2)) # 内存地址一样是同一个对象 5 6 lst1 = ["紫衫龙王","白眉鹰王","青翼蝠王","金毛狮王"] 7 lst2 = lst1.copy() # 拷贝 克隆 复制 8 lst2.append("张无忌") 9 print(id(lst1),id(lst2)) 10 print(lst1,lst2) 11 12 lst1 = ["紫衫龙王","白眉鹰王","青翼蝠王","金毛狮王"] 13 lst2 = lst1[:] 14 print(id(lst1),id(lst2)) 15 16 lst1 = ["杨晓", "谢逊", ["说不得","听不得","打不得","骂不得"]] 17 lst2 = lst1.copy() # 拿到的是第一层的复制品 18 print(id(lst1[2]),id(lst2[2])) 19 lst1[2].append("看不得") 20 print(lst1,lst2) 21 import copy 22 lst1 = ["杨晓", "谢逊", ["说不得","听不得","打不得","骂不得"]] 23 lst2 = copy.deepcopy(lst1) # 深度copy 24 print(id(lst1[2]), id(lst2[2])) 25 lst1[2].append("看不得") 26 print(lst1, lst2)
python-fullstack-s13-day07-python基础
标签:== while循环 基础 添加 color 学习 保存 改变 append
原文地址:https://www.cnblogs.com/bug-ming/p/9150623.html