2017年2月18日 星期六

Python 字典

空字典
abc = {}

增加鍵-值
abc['color'] = 'green'
abc['fruit'] = 'apple'

abc = {'color': 'green' , 'fruit': 'apple'}

走訪所有鍵-值
for key, value in abc.items():

走訪所有鍵
for name in abc.keys():
    print(name.title() )
按順序
for name in sorted(abc.keys() ):


走訪所有值
for name in abc.values():
    print(name.title() )
刪除重複
for name in set(abc.values() ):