Views in Python3.1

Source: Internet
Author: User

Views in Python3.1

In Python3, the role of Views is similar to the role of Views in a database, which can reflect the changes of corresponding objects. After the database table changes, the View query results are the same as the changed values. The same is true for the values queried through Views in python.


In Python3, dict. keys (), dict. items (), dict. values () returns Views instead of list. You can use the list () function to convert Views () to list.

The following is an example of the effect of Views:

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}>>> keys = dishes.keys()>>> values = dishes.values()>>> # iteration>>> n = 0>>> for val in values:...     n += val>>> print(n)504>>> # keys and values are iterated over in the same order>>> list(keys)['eggs', 'bacon', 'sausage', 'spam']>>> list(values)[2, 1, 1, 500]>>> # view objects are dynamic and reflect dict changes>>> del dishes['eggs']>>> del dishes['sausage']>>> list(keys)['spam', 'bacon']>>> # set operations>>> keys & {'eggs', 'bacon', 'salad'}{'bacon'}


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.