Generators can only yield ints?

D

defn noob

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)


Traceback (most recent call last):
File "<pyshell#225>", line 1, in <module>
l.next()
File "<pyshell#223>", line 5, in letters
yield chr(a)
TypeError: an integer is required

Any way to get around this?
 
B

bearophileHUGS

defn noob:
Any way to get around this?

Your code is wrong, this is one of the correct versions:

from itertools import izip

def letters():
lower = xrange(ord('a'), ord('z')+1)
upper = xrange(ord('A'), ord('Z')+1)
for lc, uc in izip(lower, upper):
yield chr(lc)
yield chr(uc)

print list(letters())

There are other ways to do the same thing.

Bye,
bearophile
 
M

Medardo Rodriguez (Merchise Group)

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)


TypeError: an integer is required

No. The problem is that "chr function" receives "int"
"a" and "B" are generators of "int" items.

What exactly you intent with that code?

Maybe:
#<code>
def letters():
for i in xrange(ord('a'), ord('z')+1):
yield chr(i)
for i in xrange(ord('A'), ord('Z')+1):
yield chr(i)

for letter in letters():
print letter
#</code>

Regards
 
S

Steven D'Aprano

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
while True:
yield chr(a)
yield chr(B)



Traceback (most recent call last):
File "<pyshell#225>", line 1, in <module>
l.next()
File "<pyshell#223>", line 5, in letters
yield chr(a)
TypeError: an integer is required

Any way to get around this?

Yes, write code that isn't buggy :)

Generators can return anything you want. Your problem is that you're
passing an xrange object to chr() instead of an int. Try this:

def letters():
a = xrange(ord('a'), ord('z')+1)
B = xrange(ord('A'), ord('Z')+1)
for t in zip(a, B):
yield chr(t[0])
yield chr(t[1])

But (arguably) a better way is:

def letters():
from string import ascii_letters as letters
for a,b in zip(letters[0:26], letters[26:]):
yield a
yield b


Note that it is important to use ascii_letters rather than letters,
because in some locales the number of uppercase and lowercase letters
differ.
 
L

Lie

def letters():
        a = xrange(ord('a'), ord('z')+1)
        B = xrange(ord('A'), ord('Z')+1)
        while True:
                yield chr(a)
                yield chr(B)


Traceback (most recent call last):
  File "<pyshell#225>", line 1, in <module>
    l.next()
  File "<pyshell#223>", line 5, in letters
    yield chr(a)
TypeError: an integer is required



Any way to get around this?

The most direct translation on what you've done, with corrections, is
either this:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
while True:
yield a
yield B
xrange(97, 123)

or this:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
while True:
yield [chr(char) for char in a]
yield [chr(char) for char in B]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

but my psychic ability guessed that you actually wanted this instead:


def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
a = [chr(char) for char in a]
B = [chr(char) for char in B]
index = 0
while index < 26:
yield a[index]
yield B[index]
index += 1
'a'

or possibly in a more pythonic style, using for:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
for lower, upper in zip(a, B):
yield chr(lower)
yield chr(upper)
'a'

paralelly, my psychic ability also tells me that you might prefer this
instead:

def letters():
a = xrange(ord('a'), ord('z') + 1)
B = xrange(ord('A'), ord('Z') + 1)
for lower, upper in zip(a, B):
yield chr(lower), chr(upper)
('a', 'A')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top