Operation - Dictionary

dictionary # key-value type

info = {

  ‘stu1101’: "Teng",

  'stu1102': "Long",

}

print(info) # The dict is unordered, the key must be unique, and it is inherently de-duplicated

print(info["stu1101"]) #Query

print(info.get("stu1105") #Robust query

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

info["stu1104"] = "Alex" # create

del info["Long"] #del

info.popitem() #Delete one randomly

info.pop("stut1101") #delete stut1101

print('stutter101' in info) #Determine whether it is True

 

b ={

  'stut1101": "Louie",

  1:3,

  2:55,

}

 info.update(b) #The  same key record is updated, different keys are added, 1 and 2 are new keys

print(info)

 

### string to dictionary

eval() 

 

#Multi-level dictionary nesting operation - "tree structure

 

 

#Multi-level dictionary nesting operation-"tree structure_area 

={
"SH":{"1":("Shanghai Railway Station","People's Square"),
"2":("Nanjing West Road","Zhongshan Park","Jing'an Temple")
},
"BJ":{"1":"No data",}
}
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)





-- ----------------------------- There seems to be a problem with the above ----------------- -----------------

 

catalog ={

  "math": {

    "AAA": "Algebra",

    "BBB": "Geometry",

  },

  "language": {

    "shige": "诗文",

    "sanfan": "散文",

  },

}

 catalog["language"]["shige"][1] = "Poetry is from the Tang Dynasty"  #update

 

info.values()

info.keys()

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

info.items()

c = dict.fromkeys([7,8,9]) #Initialize a new dictionary

print(c) #create dictionary is empty

d = dict.fromkeys([7,8,9],"test")  #3 share a memory address pointer, update updates all of them together

print(d)

 

#dictionary loop

for i in info #The    efficiency is higher than the following

  print(i, info[i])

for k,v in info.items():

  print (k, v)

 

### Three-level menu case

###Define three-level dictionary

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("Choose to enter >>>:")

      if  choice2 in data[choice]:

        while not exit_flag:

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

            print("\t\t", i3)

          choice3 = input("Choose to enter 3>>>:")

          if choice3  in data[choice][choice2]:

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

              print("\t\t",i4)

              choice4 = input("The last layer, press b to return >>>:")

                if choice4=="b":

                  break

                  #pass does nothing

                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"

          

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325286048&siteId=291194637