This article describes the simple usage of python iterators and is shared with you for your reference. The specific analysis is as follows:
Generator expression is an iterator writing method used to generate sequence parameters when a function call is called.
Generator objects can be traversed or converted into lists (or data structures such as tuples), but cannot be sliced. When the only actual argument of a function is an iterable sequence, you can remove the brackets at both ends of the generator expression and write more elegant code:
>>>> sum(i for i in xrange(10)) 45
Summ 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(, range(n), m) To add floating point values with extended precision, see ().
The parameters require an iterable sequence to be passed in, and we pass in a generator object, which is perfectly implemented.
Pay attention to distinguishing the following code:
The j above is the generator type, and the j below 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 be helpful to everyone's learning Python programming.