基本語法:
dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}
技巧:
字典中包含列表:dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}
字典中包含字典:dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','job':'dota'"}}
可用命令:
root@yangrong:~# cd /python
root@yangrong:/python# python
Python 2.7.5+ (default, Sep 19 2013,13:48:49)
[GCC 4.8.1] on linux2
Type "help","copyright", "credits" or "license" for moreinformation.
>>> import tab
>>> d={}
>>> d.
d.__class__( d.__ge__( d.__len__( d.__setitem__( d.has_key( d.setdefault(
d.__cmp__( d.__getattribute__( d.__lt__( d.__sizeof__( d.items( d.update(
d.__contains__( d.__getitem__( d.__ne__( d.__str__( d.iteritems( d.values(
d.__delattr__( d.__gt__( d.__new__( d.__subclasshook__( d.iterkeys( d.viewitems(
d.__delitem__( d.__hash__ d.__reduce__( d.clear( d.itervalues( d.viewkeys(
d.__doc__ d.__init__( d.__reduce_ex__( d.copy( d.keys( d.viewvalues(
d.__eq__( d.__iter__( d.__repr__( d.fromkeys( d.pop(
d.__format__( d.__le__( d.__setattr__( d.get( d.popitem(
>>> d.
增加字典元素
>>> nameinfo={}
>>> nameinfo['a1']='yangrong' #若字典裡有a1主鍵,則覆蓋原來的值,沒有,則添加
>>> nameinfo
{'a1': 'yangrong'}
遍歷字典主鍵與鍵值
>>> for k, value innameinfo.items():
... print k,value
...
a1 yangrong
查看字典所有主鍵
>>> dict = {'ob1':'computer','ob2':'mouse', 'ob3':'printer'}
>>>
>>>
>>> dict.keys()
['ob2', 'ob3', 'ob1']
判斷字典中是否有該主鍵
>>> dict.keys()
['ob2', 'ob3', 'ob1']
>>> dict.has_key('ob2') #或'ob2' in dict
True
>>> dict.has_key('ob4')
False
也有人用循環方法來判斷
for key in dict.keys():
但是這種方法畢竟不夠簡潔,
查看字典所有鍵值內容
>>> dict = {'ob1':'computer','ob2':'mouse', 'ob3':'printer'}
>>> dict.values()
['mouse', 'printer', 'computer']
列出所有項目
>>> dict.items()
[('ob2', 'mouse'), ('ob3', 'printer'),('ob1', 'computer')]
清空字典
>>> dict.clear()
>>> dict
{}
拷貝字典
>>> dict
{'ob2': 'mouse', 'ob3': 'printer', 'ob1':'computer'}
>>> a=dict
>>> a
{'ob2': 'mouse', 'ob3': 'printer', 'ob1':'computer'}
>>> b=dict.copy()
>>> b
{'ob2': 'mouse', 'ob3': 'printer', 'ob1': 'computer'}
比較字典
>>> cmp(a,b)
首先比較主鍵長度,然後比較鍵大小,然後比較鍵值大小,(第一個大返回1,小返回-1,一樣返回0)
更新字典
>>>dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','job':'dota'"}}
>>> dict
{'xiaohei':set(["'age':'22','job':'dota'"]), 'yangrong': {'age': '23', 'job':'IT'}}
>>> dict['xiaohei']=111 #修改一級字典
>>> dict
{'xiaohei': 111, 'yangrong': {'age': '23','job': 'IT'}}
>>> dict['yangrong']['age']=25 #修改二級字典
>>> dict
{'xiaohei': 111, 'yangrong': {'age': 25,'job': 'IT'}}
>>> dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}
>>>dict['xiaohei'][1]="dota2" #修改字典中列表某項,1是代表列表中第2個字符串。
>>> dict
{'xiaohei': ['22', 'dota2'], 'yangrong':['23', 'IT']}
刪除字典元素
>>> dict
{'xiaohei': ['22', 'dota2'], 'yangrong':['23', 'IT']}
>>> del dict['xiaohei'] #刪除xiaohei鍵值
>>> dict
{'yangrong': ['23', 'IT']}
>>> dict
{'yangrong': ['23', 'IT']}
>>>
>>> del dict['yangrong'][1] #刪除yangrong主鍵的每2字值
>>> dict
{'yangrong': ['23']}
刪除整個字典
>>> dict
{'yangrong': ['23']}
>>> dict.clear() #同del dict
>>> dict
{}
將字符串切分為列表
>>> s="hello world bye"
>>> s.split() #用於把有規律的文本,讀取出來後,使用列表進行修改,再寫入文件。
['hello', 'world', 'bye']
將列表轉換為字符串
S.split(str, ' ') #將string轉list,以空格切分
存儲字典(pickle序列化)
#需導入pickle模塊 import pickle
把字典內容存入文件
f=file('data.txt','wb') #新建文件data.txt,'wb',b是打開塊文件,對於設備文件有用
pickle.dump(a,f) #把a序列化存入文件
f.close()
把內容讀入字典(反序列化)
a=open('data.txt','rb') #打開文件
print pickle.load(a) #把內容全部反序列化
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡