Python iter, iterator &dict, dictionary explanation

Source: Internet
Author: User
Tags iterable

An object that can directly act on a for loop is called an iterative object (iterable).

An object that can be called by the next () function and continually returns the next value is called an iterator (Iterator).

All iterable can be converted to iterator via the built-in function iter ().

Iterators provide an interface for class sequence objects with a sequence of classes. Python's iterations seamlessly support sequence objects, and it also allows programmers to iterate over non-sequential types, including user-defined objects. Iterators are handy, you can iterate over objects that are not sequences but behave in sequence, such as dictionary keys, rows of a file, and so on. The role of iterators is as follows:

>>> i=iter (' ABCD ') >>> print i.next () a>>> print i.next () B>>>print i.next () c

  

s = {' One ': 1, ' One ': 2, ' three ': 3}print s{' three ': 3, ' both ': 2, ' one ': 1}>>> m = iter (s) >>> print m.next () t hree>>> print m.next () two>>> print m.next () one

dict {' key ': value, ' key ': value}

The query can be queried either by the Get method or by key directly, but if the key does not exist, the error will be given, and get will give none

You can add or change dict,dict (' key ') directly =value

You can use the Pop method Dict.pop (Key[,default]) to delete the elements within the dict with the key value and return the value corresponding to the deleted key. If key does not exist and the default value is not set, the Keyerror exception is returned

You can use the Clear Method Dict.clear () to empty the Dict

VALUES ()/Itervalues () method: Returns the value of Dict,

Replace the values () method with the itervalues () method, which is exactly the same as the iteration effect. The itervalues () method does not convert, and it takes the value from the dict in turn during the iteration,

So the Itervalues () method saves the memory needed to generate a list than the values () method.

Items () method

>>> s = {' One ': 1, ' both ': 2, ' three ':3}>>> d = items (s) >>> d=s.items () >>> print d[(' Three ', 3), (' One ', 2), (' One ', 1)]

and values () have a itervalues () like, items () also have a corresponding iteritems (),

Iteritems () does not convert the dict to list, but the tuple is given continuously during the iteration, so iteritems () does not occupy additional memory

 

Python iter, iterator &dict, dictionary explanation

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.