运算-Dictionary

字典 # key-value type

info = {

  ‘stu1101’: "Teng",

  'stu1102': "Long",

}

print(info) # dict是无序的,key必须唯一,天生去重

print(info["stu1101"]) #查询

print(info.get("stu1105") #稳健查询

info["stu1101"] = "TEND LAN" #udpate

info["stu1104"] = "Alex" # 创建

del info["Long"] #del

info.popitem() #随机删除一个

info.pop("stut1101") #删除stut1101

print(‘stutter101‘ in info)#判断是否Ture

 

b ={

  'stut1101": "Louie",

  1:3,

  2:55,

}

 info.update(b)  #相同的key记录更新,不同key则添加,1和2就是新的key

print(info)

 

###字符串变字典

eval() 

 

#多级字典嵌套操作-》树状结构

 

 

#多级字典嵌套操作-》树状结构

_area ={
"SH":{"1":("上海火车站","人民广场"),
"2":("南京西路","中山公园","静安寺")
},
"BJ":{"1":"没有数据",}
}
for i in _area:
print(i) # print(i,_area[i])
for j in _area[i]:
print("----",j,_area[i][j])
for h in _area[i][j]:
print("--------",h)





------------------------------- 以上似乎有问题 ----------------------------------

 

catalog ={

  "数学": {

    “AAA”: "代数”,

    “BBB”: “几何”,

  },

  “语文”: {

    "shige": "诗文",

    "sanfan": "散文",

  },

}

 catalog["语文"]["shige”][1] = "诗歌是唐朝的”  #update

 

info.values()

info.keys()

catalog.setdefualt("英语",{"English": [1,2]}) #create

info.items()

c = dict.fromkeys([7,8,9]) #初始化一个新的字典

print(c) #创建字典为空

d =  dict.fromkeys([7,8,9],"test")  #3个共享一个内存地址指针,update则一起update其中所有的内容

print(d)

 

#字典循环

for i in info    #效率比下面高

  print(i, info[i])

for k,v in info.items():

  print(k,v)

 

### 三级菜单案例

###定义三级字典

exit_flag = False

while not exit_flag : 

  for i in data:

    print(i)

  choice = input("选择>:")

  if choice in data

    while not exit_flag:

      for i2 in data[choice]:

        print("\t",i2)

      choice2 = input("选择进入>>>:")

      if  choice2 in data[choice]:

        while not exit_flag:

          for i3 in data[choice][choice2]" :

            print("\t\t", i3)

          choice3 = input("选择进入3>>>:")

          if choice3  in data[choice][choice2]:

            for i4 in data[choice][choice2][choice3] :

              print("\t\t",i4)

              choice4 = input("最后一层,按b返回>>>:")

                if choice4=="b":

                  break

                  #pass   什么也不做

                elif choice4 =="q":

                  exit_flag ="True"

          if choice3=="b":

            break

         elif choice3 =="q":

              exit_flag ="True"

      if choice2=="b":

        break

      elif choice2 =="q":

        exit_flag ="True"

          

 

posted @ 2018-05-03 22:01  ywyin  阅读(145)  评论(0编辑  收藏  举报