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