標籤:c中 form utf-8 str 學習 空格 修改 def 三級菜單
一:字典的學習
#codeing:UTF-8#__author__:Duke#date:2018/3/3/001#今天學習python中最為重要的資料類型 字典#字典的建立# 一對大括弧a = {‘name‘:‘duke‘,‘age‘:‘20‘}print(a)#增a[‘worke‘]=‘IT‘;print(a)#setdefault 方法 如果鍵存在,即返回鍵的值,不存在,即返回插入的值b = a.setdefault(‘hobby‘,‘learning‘);print(a)print(b)b = a.setdefault(‘hobby‘,‘haha‘);print(b)#查 通過鍵去查看print(a[‘name‘])#查看全部的keyprint(list(a.keys()))#修改a[‘name‘] = ‘wangwang‘;print(a);#加入c = {‘name‘:‘duke‘,‘high‘:170}a.update(c); #將a中沒有c中的鍵時,將c中的這些鍵加入a中,有相同鍵時,直接更新他的值print(a)#排序print(sorted(a.items()))#迴圈遍曆 1for i,v in a.items(): #遍曆出來的是鍵 print(i,v)#方法2for i in a: #遍曆出來的是鍵 print(i,a[i]) #刪#del 方法print(a)del a[‘name‘] #刪除指定索引值對print(a)#pop方法d = a.pop(‘age‘) #刪除指定索引值對,並返回刪除的值print(d)print(a);#popitem方法d = a.popitem() #隨機刪除一個索引值對 幾乎沒用print(d)print(a)#clear方法a.clear() #清空字典,該對象還存在print(a)# del a #直接刪除字典這個對象
二:一個三級菜單的建立小程式
#codeing:UTF-8#__author__:Duke#date:2018/3/3/003information = { ‘河南‘ :{ ‘鄭州‘:{ ‘高新區‘:{}, ‘二七廣場‘:{}, }, ‘洛陽‘:{} }, ‘北京‘ :{ ‘朝陽‘:{ ‘國貿‘:{ ‘CICC‘:{}, }, }, ‘海澱‘:{ ‘五道口‘:{ ‘Google‘:{}, ‘網易‘:{}, ‘搜狐‘:{}, ‘快手‘:{}, }, ‘中觀村‘:{ ‘youku‘:{}, ‘汽車之家‘:{}, ‘新東方‘:{}, }, }, ‘三裡屯‘:{ ‘回龍觀‘:{}, }, }, ‘上海‘:{ ‘浦東‘:{ ‘陸家嘴‘:{ ‘CICC‘:{}, ‘摩根‘:{}, }, ‘外灘‘:{}, }, ‘靜安‘:{}, ‘閩行‘:{}, }} #字典存放三級菜單資料current_layer = information; #記錄當前所在級parent_layers =[ ]; #列表存放進入路徑while True: for key in current_layer: print(key); #迴圈列印菜單 choice = input("[b:返回 ]>>>:").strip() #輸入,並去除前後的空格形式的多餘字元 if len(choice) == 0:continue; if choice in current_layer: parent_layers.append(current_layer) #進入下一層前先將他的前一層放到進入的路徑列表中 current_layer = current_layer[choice]; #將下一層賦值為當前層 elif choice == ‘b‘: if parent_layers: current_layer = parent_layers.pop (); #取出它的父層,並賦值到當前層 else: print("已經是最第一層。") else: #判斷非法輸入 print("無該選項...");
python學習day06