python-iterative __python

Source: Internet
Author: User

Traversing data, which we call iterative
For traversing data in Python, we typically use for...in

>>> d = {' A ': 1, ' B ': 2, ' C ': 3}
>>> for key in D: ...     Print (key) ...
A
c
b

This allows you to simply iterate through the data. By default, if the Dict (dictionary structure) sequence of iterations is not necessarily, and is the iteration of the key, we use for value in D.values () if we want to iterate over the value. The way if key and value are to be traversed using for key,value in D.items ()

Note that strings are also objects that can be iterated to determine whether an object can be iterated

We use the Collections module's iterable type to determine whether we can iterate

>>> from collections import iterable
>>> isinstance (' abc ', iterable) # str can iterate
True
>>> isinstance ([1,2,3], iterable) # list can iterate
True
>>> isinstance (123, iterable) # integers can be iterated
False

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.