Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python 3 print generator

There is a problem when i deal with print() function(Python 3).

When I'm looking for sum of a series I may use the following code pattern:

>>> sum(i for i in range(101))

But when I tend to check the series that I had made: (I choose print() and assume it will print out line by line)

>>> print(i for i in range(101))

It turns out become a generator object without value return. So I have to used list() for series checking. Is that a flaw in print function?

PS: The above written is an example to form a generator, not the simplest form for natural series but the bone structure for complex series. In order for convenience in series values checking, I am looking for a way to print out each value line by line.

like image 345
nobody Avatar asked Aug 01 '14 04:08

nobody


People also ask

How do I print a generator in Python?

Use list() to print a generator expression. Call list(object) with object as the generator expression to create a fully computed list of the generator's output. Call print(list) with list as the previous result to print the generator expression.

Can you unpack a generator Python?

You can carry out the unpacking procedure for all kinds of iterables like lists, tuples, strings, iterators and generators. There are 2 ways to unpack iterables in Python. For known length iterables - Providing the exact number of variables to unpack as the number of elements in the sequence/iterable.

How do you access the generator object in Python?

You need to call next() or loop through the generator object to access the values produced by the generator expression. When there isn't the next value in the generator object, a StopIteration exception is thrown. A for loop can be used to iterate the generator object.

What is generator in Python with example?

Python provides a generator to create your own iterator function. A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a sequence of values. In a generator function, a yield statement is used rather than a return statement.


2 Answers

sum takes an iterable of things to add up, while print takes separate arguments to print. If you want to feed all the generator's items to print separately, use * notation:

print(*(i for i in range(1, 101)))

You don't actually need the generator in either case, though:

sum(range(1, 101))
print(*range(1, 101))

If you want them on separate lines, you're expecting the behavior of multiple individual calls to print, which means you're expecting the behavior of a regular loop:

for item in generator_or_range_or_whatever:
    print(item)

though you also have the option of specifying '\n' as an item separator:

print(*generator_or_range_or_whatever, sep='\n')
like image 98
user2357112 supports Monica Avatar answered Oct 01 '22 09:10

user2357112 supports Monica


This behavior isn't too much different than on python2.x:

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print (i*i for i in range(30))
<generator object <genexpr> at 0x10c034a50>

Generally speaking, if you want to actually know the items, a list might be easiest (it just requires the addition of a couple square brackets:

print [i*i for i in range(30)]

or on python3.x:

print([i*i for i in range(30)])
like image 23
mgilson Avatar answered Oct 01 '22 09:10

mgilson