bloggerads

2017年8月3日 星期四

Python : dictionary

Follows are the Dictionary example. Key and value can be string or number.

● Initial a dictionary
>>> d={'a':'Martin', 'b':'John'}

● Add new item
>>> d['c']='Mary'

 Add items
>>> d.update({'d':'Megan', 'e':'May'})

 Check item in a dictionary (return True/False)
>>> 'd' in d
True
>>> 'f' in d
False

 Enumerate the items in a dictionary
>>> for id, name in d.items():
...         print id, name

 Delete an item
>>> del d['a']

沒有留言:

張貼留言