[Newbie] List from a generator function

  • Thread starter =?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=
  • Start date
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

Hi all,

I'm sure there is a better way to do this:

[random.choice(possible_notes) for x in range(length)]

Regards,
Ernesto
 
M

Marc 'BlackJack' Rintsch

I'm sure there is a better way to do this:

[random.choice(possible_notes) for x in range(length)]

There is at least a better way to ask the question. The subject has
nothing to do with the body of your post. Or am I missing something?

What is `length`? Do you want unique elements from `possible_notes`?
Then you could use `random.sample()`.

Ciao,
Marc 'BlackJack' Rintsch
 
G

Guest

I'm sure there is a better way to do this:
[random.choice(possible_notes) for x in range(length)]
There is at least a better way to ask the question. The subject has
nothing to do with the body of your post. Or am I missing something?

Sorry, I should have explained better. I just want to build a fix length
list made up of elements generated by a function, in this case
random.choice(). I don't like my list comprehension, because I'm using
that dumb variable x and the range() list.

Ernesto
 
P

Peter Otten

Ernesto said:
I'm sure there is a better way to do this:

[random.choice(possible_notes) for x in range(length)]

Note that "generator" has a fixed meaning in Python:
http://www.python.org/dev/peps/pep-0255/

For generators you can use

list(itertools.islice(gen()), length)

What you need then would be a way to turn an ordinary function into a
generator:
.... def gen():
.... while 1:
.... yield fun(*args, **kw)
.... return gen()
....['e', 'b', 'b', 'a', 'b', 'b', 'a']

Peter
 
P

Paul Rubin

Ernesto García García said:
[random.choice(possible_notes) for x in range(length)]
There is at least a better way to ask the question. The subject has
nothing to do with the body of your post. Or am I missing something?

Sorry, I should have explained better. I just want to build a fix
length list made up of elements generated by a function, in this case
random.choice(). I don't like my list comprehension, because I'm using
that dumb variable x and the range() list.

Use xrange instead of range. If you want to do it with no variables,
hmmm:

from itertools import islice, starmap, repeat
import random

possible_notes = range(12)
length = 9

print list(islice(starmap(random.choice, repeat((possible_notes,))), length))

[10, 0, 6, 7, 8, 1, 9, 6, 11]

Maybe you're sorry you asked ;)
 
?

=?ISO-8859-1?Q?Ernesto_Garc=EDa_Garc=EDa?=

Thank you guys.

So the answer is to keep with the original form, perhaps with xrange.

Ernesto
 
S

Sion Arrowsmith

Paul Rubin said:
print list(islice(starmap(random.choice, repeat((possible_notes,))), length))

Why the use of starmap() rather than
imap(random.choice, repeat(possible_notes))
?
 
P

Paul Rubin

Sion Arrowsmith said:
Why the use of starmap() rather than
imap(random.choice, repeat(possible_notes))
?

Hmm, not sure what I was thinking. I remember "imap... no that won't
work... ok, starmap". It was late. It's late now.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top