Python iterator instance analysis, python instance analysis

Source: Internet
Author: User

Python iterator instance analysis, python instance analysis

This article describes the simple usage of the python iterator for your reference. The specific analysis is as follows:

Generator expressions are used to generate sequence parameters for function calls.

Generator objects can be traversed or converted to lists (or data structures such as tuples), but cannot be slicing ). When the only real parameter of a function is an iterative sequence, you can remove the parentheses at both ends of the generator expression> to write more elegant code:

>>>> sum(i for i in xrange(10)) 45

Sum statement:

Sum (iterable [, start])
Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. the iterable's items are normally numbers, and are not allowed to be strings. the fast, correct way to concatenate a sequence of strings is by calling ''. join (sequence ). note that sum (range (n), m) is equivalent to reduce (operator. add, range (n), m) To add floating point values with extended precision, see math. fsum ().

The parameter must be passed in an iteratable sequence. We need to pass in a generator object for perfect implementation.

Note the following code:

The above j is the generator type, and the following j is the list type:

j = (i for i in range(10)) print j,type(j) print '*'*70  j = [i for i in range(10)] print j,type(j) 

Result:

<generator object <genexpr> at 0x01CB1A30> <type 'generator'>**********************************************************************[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <type 'list'>

I hope this article will help you learn Python programming.


How to Use the iterator in python?

List = [1, 2, 3, 4, 5, 6]
For item in list:
Print item

How to Use the iterator in python? For example,

Are you talking about enumerate?

The simplest example is as follows:
ListValue = ["a", "B", "c"]
For index, strValue in enumerate (listValue ):
Print index, "is", strValue

The result is:
0 is
1 is B
2 is c
 

Related Article

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.