Summary of usage of python dictionary operations

高洛峰
Release: 2017-03-28 17:14:03
Original
1463 people have browsed it

Basic syntax:

dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}

Tips:

The dictionary contains lists: dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}

The dictionary contains dictionaries: dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','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.lt( d.sizeof( d.items( 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.doc d.init( d.reduce_ex( d .copy (           

d.format(       d.le ( d.setattr( d.get( d.popitem( d.popitem() {}

>>> nameinfo['a1']='yangrong' #If there is a1 primary key in the dictionary, overwrite the original value, if not, add

> >> nameinfo

{'a1': 'yangrong'}

Traverse the dictionary primary key and key value

>>> for k, value innameinfo. items():

... print k,value

...

a1 yangrong

View all primary keys in the dictionary

>>> dict = {'ob1':'computer','ob2' :'mouse', 'ob3':'printer'}

>>>

>>>

>>> dict .keys()

['ob2', 'ob3', 'ob1']

Determine whether the primary key exists in the dictionary

>>> dict. keys()

['ob2', 'ob3', 'ob1']

>>> dict.has_key('ob2') #or'ob2' in dict

True

>>> dict.has_key('ob4')

False

Some people also use the loop method To judge

for key in dict.keys():

But this method is not concise enough after all,

View all key value contents of the dictionary

> ;>> dict = {'ob1':'computer','ob2':'mouse', 'ob3':'printer'}

>>> dict.values()

['mouse', 'printer', 'computer']

List all items

>>> dict.items()

[('ob2', 'mouse'), ('ob3', 'printer'),('ob1', 'computer')]

Clear the 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 dictionary

>>> cmp(a,b)

First compare the primary key length, then compare the key size, and then compare the key value size, (No. A large one returns 1, a small one returns -1, the same returns 0)

Update dictionary

>>>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 #Modify the first-level dictionary

>>> dict

{'xiaohei ': 111, 'yangrong': {'age': '23','job': 'IT'}}

>>> dict['yangrong']['age']= 25 #Modify the secondary dictionary

>>> dict

{'xiaohei': 111, 'yangrong': {'age': 25,'job': 'IT' }}

>>> dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}

>>>dict['xiaohei'][1]="dota2" #Modify an item in the list in the dictionary, 1 represents the second string in the list.

>>> dict

{'xiaohei': ['22', 'dota2'], 'yangrong':['23', 'IT']}

Delete dictionary elements

>>> dict

{'xiaohei': ['22', 'dota2'], 'yangrong':['23', 'IT']}

>>> del dict['xiaohei']           #Delete xiaohei key value

>>> dict

{' yangrong': ['23', 'IT']}

>>> dict

{'yangrong': ['23', 'IT']}

>>>

>>> del dict['yangrong'][1] #Delete every 2-character value of the yangrong primary key

>> > dict

{'yangrong': ['23']}

Delete the entire dictionary

>>> dict

{' yangrong': ['23']}

>>> dict.clear() #Same as del dict

>>> dict

{ }

Split the string into a list

>>> s="hello world bye"

>>> s.split() # Used to read regular text, modify it using a list, and then write it to the file.

['hello', 'world', 'bye']

Convert the list to a string

S.split(str, ' ') #WillstringConvertlist, split by spaces

Storage dictionary (pickle serialization)

#Need to import the pickle module import pickle

Save the dictionary content to the file

f=file('data.txt','wb') #Create a new file data.txt, 'wb', b is an open block file, for Device files are useful

pickle.dump(a,f) #Serialize a and store it in the file

f.close()

Read the content into the dictionary (deserialize ization)

a=open('data.txt','rb') #Open file

print pickle.load(a) #Deserialize all contents

The above is the detailed content of Summary of usage of python dictionary operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template