Why is my for loop printing memory location [duplicate] - python

N = [1, 2, 3]
print(n for n in N)
Results:
<generator object <genexpr> at 0x000000000108E780>
Why didn't it print?:
1
2
3
However the code:
sum(n for n in N)
Will sum up all the number in N.
Could you please tell me why sum() worked but print() failed?

It's because you passed a generator to a function and that's what __repr__ method of this generator returns. If you want to print what it would generate, you can use:
print(*N, sep='\n') # * will unpack the generator
or
print('\n'.join(map(str, N)))
Note that once you retrieve the generator's output to print it, the generator is exhausted - trying to iterate over it again will produce no items.

If you don't want to cast it as a list, you can try:
print(*(n for n in N))
See: https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments

You are literally printing a generator object representation
If you want on one line, try printing a list
print([n for n in N])
Which is just print(N)
If you want a line separated string, print that
print("\n".join(map(str, N)))
Or write a regular loop and don't micro optimize the lines of code

Generator …
def genfun():
yield ‘A’
yield ‘B’
yield ‘C’
g=genfun()
print(next(g))= it will print 0th index .
print(next(g))= it will print 1st index.
print(next(g))= it will print 2nd index.
print(next(g))= it will print 3rd index But here in this case it will give Error as 3rd element is not there
So , prevent from this error we will use for loop as below .
for i in g :
print(i)

Related

Generator error in python

I'm still new to generators in python. I was trying out one on my own and tried to something really simple:
def fib(a):
... if a==0 or a==1:return 1
... yield fib(a-1)+fib(a-2)
print(list(fib(5))
This code gave me this error:
TypeError: unsupported operand type(s) for +: 'generator' and 'generator'
Can't generators be used in this manner?
Calling a generator function doesn't produce the next value. It produces a generator object, a specialist version of an iterator object. You have, in effect, something that wraps a paused function.
To get another value from that function, you'd have to call next(iterator) on the object or use something like list(iteratort) or for ... in iterator to loop over the object and get the values out. See What does the "yield" keyword do? for more details.
So here, the you'd have to use next(fib(a-1)) + next(fib(a-2)) to get the two recursive values out. That'll also fail, because your termination case (a == 0 or a == 1) uses return (translated into the value of a StopIteration exception) and not yield; you'd have to fix that too.
And this highlights why your recursive function should not be a generator function. Your function doesn't produce a series of values to iterate over. There's just one result mfor a given argument value. You'd be far better off to just use return and not yield.
If you wanted to generate a sequence of fibonacci numbers, the function argument would need to be seen as a limit; "give me the first n fibonacci numbers". The following iterative function does that:
def first_n_fibonacci(n):
a, b = 0, 1
for i in range(0, n):
a, b = b, a + b
yield a
This would give you a list of the first n fibonacci numbers, as a generator:
>>> f = first_n_fibonacci(5)
>>> f
<generator object first_n_fibonacci at 0x10b2c8678>
>>> next(f)
1
>>> next(f)
1
>>> list(f)
[2, 3, 5]
or you could use the argument to produce all fibonacci values up to a limit, or to produce an endless generator of fibonacci numbers. None of those would require recursion, a loop like the above suffices.
Generators are meant to be used for iterations, and yet by adding two of the returning values from fib you are trying to use it as a scalar value, which is confirmed by your terminal condition (where a equals to 0 or 1) also returning a scalar value.
You should simply use return in this case.
def fib(a):
if a==0 or a==1:return 1
return fib(a-1)+fib(a-2)
print(fib(5))
If you do want to use a generator, you need to think about outputting a sequence, and not a single value. Do you want your fib(a) to output the ath fib number or the the 1st, 2nd, 3rd, 4th, 5th ... ath fib number? If the latter, then generators are good for this.
Here is an example generator for the Fibonacci numbers from the 1st to the nth number.
def fib(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b

Generating unique Pythagorean triplets using this algorithm

def pyg(n):
n=int(n)
for i in range(1,n):
a=(2*i)+1
b=(2*i)*(i+1)
c=(2*i)*(i+1)+1
return(a,b,c)
When i try to run this on Shell using pyg(100) I only get output (3, 4, 5).
What might be the problem all the triplets are not generating.
As many others pointed out, when the return statement executes, your function will finish, and will not continue looping. However, you have options for returning multiple values from a single function. For example aggregating your values in a list and return that, or to use the yield keyword instead of return. The yield keyword will make your function a generator function, which returns an iterable generator object with all your expected elements.
I advise you to split your code into separate functions, so you have the original formula as a function of i, and a generator function which will return the elements for 1 <= i < n. Note that you can collect the elements of a generator to a list by supplying it to the list constructor.
def pyg(i):
a = (2*i) + 1
b = (2*i) * (i+1)
c = (2*i) * (i+1) + 1
return (a,b,c)
def pygs(n):
for i in range(1, n):
yield pyg(i)
print(list(pygs(10))) # prints the first 9 Pythagorean triplet as a list of tuples
The return sentence. Change it for a yield. If you want to test it, you could:
>>> pygs = pyg(100)
>>> next(pygs)
(3, 4, 5)
>>> next(pygs)
.
.
.
Your function produces only one pair, because return interrupts function execution. You could use yield keyword, as suggested by #jgomo3, or map function:
def generate_triplet(n):
a=(2*n)+1
b=(2*n)*(n+1)
c=(2*n)*(n+1)+1
return(a,b,c)
def pyg(n):
return map(generate_triplet, range(1, int(n))

How does a for loop know to increment

When defining a Fibonacci sequence the following code was used:
def fibonacci(n):
current=0
after=1
for i in range(1,n):
current,after=after,current+after
return after
How does the for loop know to increment by one every time we pass through? I tried using a while loop while e<=n: and it returned an error as I hadn't defined e.
A for loop in python iterates through items or generators. In your example, range(1,n) will return a list of elements between [1, n) in python2, or a generator that will yield the same items in python3.
Essentially, a for loop can iterate any kind of iterables:
for item in [1, 6, 8, 9]:
print(item)
It will print 1, 6, 8 and 9.
Using range, there is a 3rd optional parameter which allows you to specify the increment:
range(1, 10, 1) # The defaut [1 .. 9] numbers
range(1, 10, 2) # From 1 until 9 with 2 of increment
for loop does not increment, instead it iterates
The for loop does not increment, instead it is asking so called iterable to provide values to work with.
In your example, the iterable is range(1,n).
In Python 2.x, the range(1, n) creates a list of values and the for loop is only asking the list to provide next value in each run, until the list gets exhausted from values.
In Python 3.x the range(1, n) is optimized and returns special iterable type <range>, which when asked by for loop to provide next value provide it.
By default if you don't specify your step i.e for i in range (start,stop,step) the increment is considered as one otherwise it's your step that you specified.

python error: generator object P at 0x02DAC198

I'm using Python 3.4.* and I am trying to execute the following code:
def P(n):
if n == 0:
yield []
return
for p in P(n-1):
p.append(1)
yield p
p.pop()
if p and (len(p) < 2 or p[-2] > p[-1]):
p[-1] += 1
yield p
print(P(5)) # this line doesn't make sense
for i in P(5): # but this line does make sense thanks to furkle
print(i)
but I am getting <generator object P at 0x02DAC198> rather than the output.
Can someone explain where in my code needs to be fixed? I don't think py likes the function name P but I could be wrong.
Edit: furkle clarified <generator object P at 0x02DAC198>.
By the way, I'm currently trying to write my own modified partition function and I was trying to understand this one corresponding to the classical setting.
I think you're misunderstanding the concept of a generator. A generator object is like a list, but you can iterate through its results lazily, without having to wait for the whole list to be constructed. Calling an operation on a function that returns a generator will not perform that operation in sequence on every item yielded by the generator.
If you wanted to print all the output of P(5), you should write:
for i in P(5):
print(i)
If you just want to print a list of the content returned by the generator, that largely seems to defeat the purpose of the generator.
Many things are wrong with this code, and your understanding of how generators work and what they are used for.
First, with respect to your print statement, that is exactly what it should print. Generators are never implicitly expanded, because there is no guarantee that a generator would ever terminate. It's perfectly valid, and sometimes very desirable, to construct a generator that produces an endless sequence. To get what you'd want (which I assume is produce output similar to a list), you'd do:
print(list(P(5))
But that brings me to my second point; Generators yield values in a sequence (in 99% of uses for them, unless you're using it as a coroutine). You are trying to use your generator to construct a list; however, if n is not 0 this will never yield a value and will immediately return. If you goal is to construct a generator that makes a list of 1's of a given length, it should look like this:
def P(n):
while n >= 0:
yield n
n -=1
This will produce a sequence of 1's of length n. To get the list form, you'd do list(P(n)).
I suggest you have another read over the Generator Documentation and get a better feel for them and see if they're really the right tool for the job.
In reading the function, I try to find what the call will produce. Let's start with the full original code:
def P(n):
if n == 0:
yield []
return
for p in P(n-1):
p.append(1)
yield p
p.pop()
if p and (len(p) < 2 or p[-2] > p[-1]):
p[-1] += 1
yield p
print(P(5)) # this line doesn't make sense
Okay, so it calls P(5). Since that's not 0, P recurses, until we reach P(0) which yields an empty list. That's the first time p receives a value. Then P(1) appends 1 into that list, and yields it to P(2) which repeats the process.. and so on. All the same list, originally created by P(0), and eventually yielded out as [1,1,1,1,1] by P(5) - but then the magic happens. Let's call this first list l0.
When you ask the generator for the second item, control returns to P(5) which now removes a value from l0. Depending on a bunch of conditions, it may increment the last value and yield p, which is l0, again. So the first item we received has been changing while we asked for the second. This will eventually terminate, but means there's a difference between these two:
print list(P(5)) # Eventually prints a list of l0 which has been emptied!
for item in P(5):
print item # Prints l0 at each point it was yielded
In [225]: for i in P(5): print i
[1, 1, 1, 1, 1]
[2, 1, 1, 1]
[2, 2, 1]
[3, 1, 1]
[3, 2]
[4, 1]
[5]
In [226]: list(P(5))
Out[226]: [[], [], [], [], [], [], []]
This is why I called it post-modifying; the values it returns keep changing after they've been produced (since they are in fact the same object being manipulated).

Strange python for syntax, how does this work, whats it called?

print max(3 for i in range(4))
#output is 3
Using Python 2.6
The 3 is throwing me off, heres my attempt at explaining whats going on.
for i in range(4) makes a loop that loops 4 times, incrementing i from 0 to 3 at the start of each loop. [no idea what the 3 means in this context...] max() returns the biggest iterable passed to it and the result is printed to screen.
3 for i in range(4) is a generator that yields 3 four times in a row and max takes an iterable and returns the element with the highest value, which is, obviously, three here.
This evaluates to:
print max([3,3,3,3])
... which is an elaborate way to say print 3.
expr for x in xs is a generator expression. Typically, you would use x in expr. For example:
[2*i for i in range(4)] #=> [0, 2, 4, 6]
It can be rewritten as:
nums = []
for i in range(4):
nums.append(3)
print max(nums) # 3! Hurrah!
I hope that makes its pointlessness more obvious.
The expression:
print max(3 for i in range(4))
is printing the result of the max() function, applied to what is inside the parentheses. Inside the parentheses however, you have a generator expression creating something similar to an array, with all elements equal to 3, but in a more efficient way than the expression:
print max([3 for i in range(4)])
which will create an array of 3s and destroy it after it is no longer needed.
Basically: because inside the parentheses you will create only values that are equal, and the max() function returns the biggest one, you do not need to create more than one element. Because with the number of elements always equal to one, the max() function becomes not needed and your code can be effectively replaced (at least in the case you have given) by the following code:
print 3
That is simply all ;)
To read more about differences between comprehension and generator expression, you can visit this documentation page.

Categories

Resources