Python基礎:字典操做

# zip生成字典
num_set = ('a','b','c','d')
word_list = ['apple', 'bat', 'cat', 'dog']
print(list(zip(num_set,word_list)))
to_dict = dict(zip(num_set,word_list))
print(to_dict)

#字典的訪問、添加、修改、刪除
var = to_dict['a']
print(var)
to_dict['e'] = 'eye'
print(to_dict)
to_dict['b'] = 'bag'
del to_dict['c']
print(to_dict)

#字典經常使用函數
to_dict1 = to_dict.copy()
print(to_dict1)

res = to_dict.keys()
print(res)

res = to_dict.values()
print(res)
print(to_dict.items())

#to_dict.clear()

#fromkeys()  根據指定的鍵和特定的一個值製做一個字典
key = ['z','y','x']
res = {}.fromkeys(key,'hiha')
print(res)
相關文章
相關標籤/搜索