python學習day06

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.