Python Dictionary basic operations

Source: Internet
Author: User

The dictionary in Python belongs to an advanced data structure, which is based on the hash table, and the complexity of the hash table in finding an element is basically O (1), so it is a very useful data structure. The following is a brief introduction to the operation of some dictionaries of capital

Create a dictionary, assign a value to a dictionary

namerank={}namerank={1:ada,2:aka,3:jia,4:gxy}

There are two ways to create a dictionary, you can directly initialize the assignment, or you can create directly, the dictionary assignment operation is as follows

Namerank[key]=value

Second, the basic operation of the dictionary

1. Visit
Adict[key] Returns the value of key key, if key is not in the dictionary, a keyerror is raised.

2. Check key

A, Has_key () method shape: Adict.has_key (' name ') has –>true, no –>false
b, in, No in shape: ' Name ' in adict with –>true, no –>false

3. Update the Dictionary

Adict[new_key] = value form adds an item

4. Delete
Del Adict[key] Delete key key item/del adict delete entire dictionary
Adict.pop (key) deletes the key keys and returns the value corresponding to key

Third, the correlation mapping function

1, Len () returns the length of the dictionary
2, hash () returns the hash value of the object, which can be used to determine whether an object can be used as a dictionary key

Iv. Methods of the dictionary

1, Adict.keys () returns a list containing all keys of the dictionary;
2, Adict.values () returns a list containing all the value of the dictionary;
3, Adict.items () returns a list containing all (key, value) Ganso;
4, adict.clear () Delete all the items or elements in the dictionary;
5, Adict.copy () returns a copy of a shallow copy of a dictionary;
6, Adict.fromkeys (seq, val=none) creates and returns a new dictionary, with the element in SEQ making the dictionary key, and Val does the initial value corresponding to all the keys in the dictionary (default is None);
7, Adict.get (key, default = None) returns the value corresponding to the key in the dictionary, and returns the value of default if the key does not exist in the dictionary (default defaults to none);
8, Adict.has_key (key) If key is in the dictionary, returns True, otherwise false. Now with in, not in;
9, Adict.iteritems (), Adict.iterkeys (), Adict.itervalues () and their corresponding non-iterative methods, the difference is that they return an iteration, rather than a list;
10, Adict.pop (Key[,default]) is similar to the Get method. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;
11, Adict.setdefault (key, Default=none) and set () method similar, but if the dictionary does not exist in the key key, by adict[key] = default for it assignment;
12, Adict.update (bdict) adds the dictionary bdict key-value pairs to the dictionary adict.

V. The traversal of a dictionary

 forValueinchAdict.values ():PrintValue#Traversing Values forKeyinchAdict.keys ():PrintKey#Traverse Key forIteminchAdict.items ():PrintItem#Traverse each item forItem,valueinchAdict.items ():Print 'key=%s, value=%s'% (Item,value)#traversing key-value pairs forItem,valueinchAdict.iteritems ():Print 'key=%s, value=%s'% (Item,value)#traversing key-value pairs

Python Dictionary basic operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.