Re: [Newbie] List from a generator function
Paul Rubin http://[EMAIL PROTECTED] wrote: print list(islice(starmap(random.choice, repeat((possible_notes,))), length)) Why the use of starmap() rather than imap(random.choice, repeat(possible_notes)) ? -- \S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/ ___ | Frankly I have no feelings towards penguins one way or the other \X/ |-- Arthur C. Clarke her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Sion Arrowsmith [EMAIL PROTECTED] writes: 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. -- http://mail.python.org/mailman/listinfo/python-list
[Newbie] List from a generator function
Hi all, I'm sure there is a better way to do this: [random.choice(possible_notes) for x in range(length)] Regards, Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
In [EMAIL PROTECTED], Ernesto García García wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Ernesto García García [EMAIL PROTECTED] writes: [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)) ## working on region in file /usr/tmp/python-21885hGZ... [10, 0, 6, 7, 8, 1, 9, 6, 11] Maybe you're sorry you asked ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Ernesto García García wrote: 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 make_gen(fun, *args, **kw): ... def gen(): ... while 1: ... yield fun(*args, **kw) ... return gen() ... from random import choice from itertools import islice length = 7 sample = abcde list(islice(make_gen(choice, sample), length)) ['e', 'b', 'b', 'a', 'b', 'b', 'a'] Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: [Newbie] List from a generator function
Thank you guys. So the answer is to keep with the original form, perhaps with xrange. Ernesto -- http://mail.python.org/mailman/listinfo/python-list