一、上课体验及感受
额第三天了。身心疲惫。上课一脸懵逼,是最标准的诠释。三个星期的疲惫感,更深了。很难,但是还要继续 写好作业。吸收知识。
二、三级列表
menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, 'youku':{}, }, '上地':{ '百度':{}, }, }, '昌平':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{}, '回龙观':{}, }, '朝阳':{}, '东城':{}, }, '上海':{ '闵行':{ "人民广场":{ '炸鸡店':{} } }, '闸北':{ '火车战':{ '携程':{} } }, '浦东':{}, }, '山东':{}, } current_level = menu last_levels = [] while True: for key in current_level: print(key) choice = input('>>>:').strip() if len(choice) == 0: continue if choice == 'b': if len(last_levels) == 0:break current_level = last_levels[-1] last_levels.pop() if choice not in current_level :continue last_levels.append(current_level) current_level = current_level[choice]
三、字典
dic = {'name':'alex ','age':18} print(dic.get('name')) #查看 dic['gender'] = 'female' #增加 dic.setdefault('gender','male') #增加一对值 dic['name'] = 'leg' #改 del dic['name'] #删除 dic.pop('name') #删除 dic.popitem() #随机删除,不能写参数 dic.clear() #清空 dic = {'name':'alex','age':18,'gfs':['monkey','Cythrina','Rachel']} dic3=dic.copy() #拷贝浅copy 只能copy1层。这里坑。如果是列表的话 内存地址是一样的。 dic3['name'] = 'jack' dic3['gfs'][2] = 'gangnnian' import copy dic4 = copy.deepcopy(dic) #深copy,内存地址不同 print(dic,dic3,dic4) dic2=dict.fromkeys('abc',1) #快速生成字典 data = dict.fromkeys([1,2,3],'alex') dic.items() #打印key和值,数据大时别使用 for k,v in dic.items(): print(k,v) print(dic.keys()) #取key for i in dic.keys(): print(i) dic1={'gender':'male'} #dic.update('name'='houziyu','age'=18) dic.update(dic1) #同key覆盖,不同添加 print(dic.values()) #取值 for i in dic.values(): print(i) #key 的定义规则1。不可变:数字,字符串,元组。可变 数组,列表 #value 定义规则:任意类型 #age=18--->int()--->__init__() #dic={}--->dict()--->__init__() dic3 = dict() print(dic3) dic4 = dict(name='alex',age=18) print(dic4) dic5=dict({'name':'alex','age':18}) print(dic5) # dic.clear()
四、字符编码
#高1 #低0 #计算机只认识数字 #字符 ----翻译过程----》数字 #1.内存固定使用unicode 编码,硬盘的编码(即你可以修改的软件编码) #2.使用什么编码往硬盘存,就用什么编码去读 #3.程序运行分两阶段:1.从硬盘读到内存2.python解释器运行已经读到内存的代码 #4.针对一个test.py文件来说python与nodpad++\vim的区别是多了第二步骤
五、文件的处理
文件内容
sadasdsaas as d asd asd ad 日了狗 das dasd as das das dasddasdasdsa
文件处理及相关参数
#data = open("lyrics",encoding='utf-8').read()# read all f = open('lyrics') line_nu = 0 for line in f: if line_nu <5: print(line.strip()) line_nu +=1 else: break for i in range(5): #一行一行的读 print(f.readline().strip()) f = open('myfile','r+') # r+ 读写。w+写读。a+ 追加读 # f = open('myfile','r') #r = read 读模式,默认模式 # f = open('myfile','a') #a = append 追加模式 # f = open('myfile','w') # w = write创建写模式。没有文件会创建。有的话会覆盖 f.write("我爱北京天安门") f.write("我爱烫头") f.close() print(f.closed) #判断文件是否关闭。 #f.encoding #查看文件编码 f.fileno() #他没说,网络编程的时候说 f.flush() #实时刷入到硬盘。 f.isatty() #忘记他吧 #f.name #打印文件名 f.seek(10) #寻找第十个字符 print(f.tell()) #当前文件光标打印的位置 f.truncate() #截断 f = open('lyrics','r') f2 = open('lyrics_new','w') find_str='日了狗' new_str = '闭嘴' for line in f : if find_str in line: line = line.replace(find_str,new_str) f2.write(line) f.close() f2.close() #eval 把一个是字符串的列表 变成列表