Basic syntax:
Dict = {' Ob1 ': ' Computer ', ' ob2 ': ' Mouse ', ' ob3 ': ' Printer '}
Skills:
Dictionary contains list: dict={' yangrong ': [' ['] ', ' IT '], ' xiaohei ': ['] ', ' DotA '}
Dictionary contains dictionary: dict={' yangrong ': {"Age": "" "," "Job": "IT"}, "Xiaohei": {"' Age ': ' '", ' job ': ' DotA '}}
Available commands:
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.
Add dictionary elements
>>> nameinfo={}
>>> nameinfo[' A1 ']= ' Yangrong ' #若字典里有a1主键, then overwrite the original value, no, add
>>> Nameinfo
{' A1 ': ' Yangrong '}
Traverse Dictionary primary key and key value
>>> for K, Value Innameinfo.items ():
... print K,value
...
A1 Yangrong
View dictionary all primary keys
>>> dict = {' Ob1 ': ' Computer ', ' ob2 ': ' Mouse ', ' ob3 ': ' Printer '}
>>>
>>>
>>> Dict.keys ()
[' Ob2 ', ' ob3 ', ' ob1 ']
Determine if the primary key is in the dictionary
>>> Dict.keys ()
[' Ob2 ', ' ob3 ', ' ob1 ']
>>> dict.has_key (' ob2 ') #或 ' ob2 ' in Dict
True
>>> dict.has_key (' ob4 ')
False
Some people use cyclic methods to judge
For key in Dict.keys ():
But this approach is not simple enough,
View dictionary all key value contents
>>> dict = {' Ob1 ': ' Computer ', ' ob2 ': ' Mouse ', ' ob3 ': ' Printer '}
>>> dict.values ()
[' Mouse ', ' printer ', ' computer ']
List all items
>>> Dict.items ()
[(' Ob2 ', ' Mouse '), (' Ob3 ', ' Printer '), (' Ob1 ', ' computer ')]
Empty dictionary
>>> Dict.clear ()
>>> Dict
{}
Copy Dictionary
>>> 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 '}
Compare dictionaries
>>> CMP (A, B)
First compare the primary key length, then compare the key size, then compare the key value size, (first large return 1, small return-1, like return 0)
Update dictionary
>>>dict={' Yangrong ': {"age": "All", "job": "IT"}, "Xiaohei": {"' Age ': ' '", ' job ': ' DotA '}}
>>> Dict
{' Xiaohei ': Set (["' Age ': ' ['] ', ' job ': ' DotA '"]), ' Yangrong ': {' age ': ' ' ", ' job ': ' IT '}}
>>> dict[' Xiaohei ']=111 #修改一级字典
>>> Dict
{' Xiaohei ': 111, ' Yangrong ': {' age ': ' + ', ' job ': ' IT '}}
>>> dict[' Yangrong ' [' Age ']=25 #修改二级字典
>>> Dict
{' Xiaohei ': 111, ' Yangrong ': {' age ': ' Job ': ' IT '}}
>>> dict={' yangrong ': [' + ', ' IT '], ' xiaohei ': ['] ', ' DotA '}
>>>dict[' Xiaohei '][1]= "Dota2" #修改字典中列表某项, 1 is the 2nd string in the representation list.
>>> Dict
{' Xiaohei ': [' + ', ' dota2 '], ' yangrong ': [' ['] ', ' IT ']}
Delete a dictionary element
>>> Dict
{' Xiaohei ': [' + ', ' dota2 '], ' yangrong ': [' ['] ', ' IT ']}
>>> del dict[' Xiaohei '] #删除xiaohei键值
>>> Dict
{' Yangrong ': [' + ', ' IT ']}
>>> Dict
{' Yangrong ': [' + ', ' IT ']}
>>>
>>> del dict[' Yangrong '][1] #删除yangrong主键的每2字值
>>> Dict
{' Yangrong ': [' 23 ']}
Delete entire dictionary
>>> Dict
{' Yangrong ': [' 23 ']}
>>> dict.clear () #同del dict
>>> Dict
{}
Cut a string into a list
>>> s= "Hello World Bye"
>>> s.split () #用于把有规律的文本, after reading, use the list to modify, and then write to the file.
[' Hello ', ' world ', ' Bye ']
Convert a list to a string
S.split (str, ') #将string转list, with a space slice
Storage dictionaries (pickle serialization)
#需导入pickle模块 Import Pickle
Deposit the contents of a dictionary into a file
F=file (' Data.txt ', ' WB ') #新建文件data. txt, ' WB ', b is open block file, useful for device files
Pickle.dump (a,f) #把a序列化存入文件
F.close ()
Read the contents into a dictionary (deserialization)
A=open (' data.txt ', ' RB ') #打开文件
Print Pickle.load (a) #把内容全部反序列化