1. 程式人生 > 其它 >字典的總結和小計

字典的總結和小計

# 題:新建一個字典項,刪除以"豬"字元開頭的項.並輸出字典結果
a = tuple(input("輸入變數a"))
dic = {
       a: "孫悟空",
       "豬八戒": "豬1",
       "豬悟能": "豬2",
       "沙和尚": "",
       "蜘蛛精": "蜘蛛",
       "唐長老": "",
       "豬大哥": "",
       }
print(dic)
# print(type(dic))
# dic.pop('孫悟空')
# print(dic)  列表的每一個元素都是一個字串
# lst = list()  # 空列表
# lst.append("孫悟空") # lst1 = ["孫悟空"] # 空列表 # print(lst[0]) # print(lst1[0]) # print(type(lst[0]), type(lst1[0])) # 檢視字典命名是否正確 # t = [] # for key1 in dic: # if dic[key1].startswith("豬"): # print(dic[key1]) # t.append(key1) # # RuntimeError: dictionary changed size during iteration #
# 錯誤,字典改變大小,在迭代區間 # # print(t) # 檢視列表裡面有些什麼 # # 迴圈臨時列表t,以t中的元素作為字典的key值進行刪除 # for t1 in t: # dic.pop(t1) # print(dic) # for key in dic: # # dic[key]直接這樣寫,這是一個變數不行,變數===變數才行,如下 # a = dic[key] # print(a) # 拿到字典中的value值. # s = {"1", "你好", "3"} # s1 = set() # print("s的資料型別", type(s)) # , "\n", "s1的資料型別", type(s1))
# # print(s) # i = 0 # for s1 in dic: # i = i + 1 # # dic[s1] # print(dic) # print("s拿出來的的資料型別", i, "項", type(s1))