Python Basic iterative __python

Source: Internet
Author: User

From:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013868196435255fcca20a1630446ea2dd434a7176e152000

Dict Default Iteration Key

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

If you want to iterate between key and value at the same time, you can use the for K, V. in D.iteritems ()

>>> for K, v. in D.iteritems ():
Print K, V
A 1
C 3
B 2

Tuple iterations

>>> for X, y,z in [(1,1,11), (2, 4,6), (3, 9,5)]:
Print X,y,z
1 1 11
2 4 6
3 9 5
How can you tell if an object is an iterative object? The method is judged by the iterable type of the collections module:

>>> 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.