471,349 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

Itertools question: how to call a function n times?

I want to write a function that each time it gets called, it returns a
random choice of 1 to 5 words from a list of words.

I can write this easily using for loops and random.choice(wordlist) and
random.randint(1, 5).

But I want to know how to do this using itertools, since I don't like
manually doing stuff like:

phrase = list()
for i in random.randint(1, 5):

phrase.append(random.choice(wordlist))

It just seems slow.

All advice welcome.

TIA

Matt

--
A better way of running series of SAS programs:
http://tplus1.com/wilsonwiki/SasAndMakefiles
Jul 19 '07 #1
4 6616
Matthew Wilson a écrit :
I want to write a function that each time it gets called, it returns a
random choice of 1 to 5 words from a list of words.

I can write this easily using for loops and random.choice(wordlist) and
random.randint(1, 5).

But I want to know how to do this using itertools, since I don't like
manually doing stuff like:

phrase = list()
for i in random.randint(1, 5):

phrase.append(random.choice(wordlist))
what's wrong with:

phrases = [random.choice(wordList) for i in random.randint(1, 5)]

Jul 19 '07 #2
Matthew Wilson wrote:
I want to write a function that each time it gets called, it returns a
random choice of 1 to 5 words from a list of words.

I can write this easily using for loops and random.choice(wordlist) and
random.randint(1, 5).

But I want to know how to do this using itertools, since I don't like
manually doing stuff like:

phrase = list()
for i in random.randint(1, 5):

phrase.append(random.choice(wordlist))
Use list comprehension:

phrase = [random.choice(wordlist) for i in xrange(random.randint(1, 5))]

w.
Jul 19 '07 #3
Matthew Wilson schrieb:
I want to write a function that each time it gets called, it returns a
random choice of 1 to 5 words from a list of words.

I can write this easily using for loops and random.choice(wordlist) and
random.randint(1, 5).

But I want to know how to do this using itertools, since I don't like
manually doing stuff like:

phrase = list()
for i in random.randint(1, 5):

phrase.append(random.choice(wordlist))

It just seems slow.

All advice welcome.

TIA

Matt
You could do it either using the previously suggested list comprehension
way or, if you don't need to have all choices in memory at once, use
generator expressions. They're basically like list comprehensions,
except that you wrap them into parentheses (), not brackets [].

phrase = (random.choice(wordlist) for i in xrange(random.randint(1, 5)))

You loose the ability to slice it directly (eg. phrase[3]), though. (See
itertools.islice for a way to do it.)

HTH,
Stargaming
Jul 19 '07 #4
On Jul 19, 8:35 am, Matthew Wilson <m...@tplus1.comwrote:
I want to write a function that each time it gets called, it returns a
random choice of 1 to 5 words from a list of words.

I can write this easily using for loops and random.choice(wordlist) and
random.randint(1, 5).

But I want to know how to do this using itertools, since I don't like
manually doing stuff like:

phrase = list()
for i in random.randint(1, 5):

phrase.append(random.choice(wordlist))
All the previous suggestions in this thread are good. If you *must*
use itertools, you can use the itertools.repeat function to return an
object x many times:

phrase = [somewords(wordlist) for somewords in
itertools.repeat(random.choice, random.randint(1, 5))]
Hope it helps,
John

Jul 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Robert Brewer | last post: by
1 post views Thread by Steven Bethard | last post: by
18 posts views Thread by Ville Vainio | last post: by
41 posts views Thread by rurpy | last post: by
23 posts views Thread by Mathias Panzenboeck | last post: by
13 posts views Thread by 7stud | last post: by
17 posts views Thread by Raymond Hettinger | last post: by
4 posts views Thread by Mensanator | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.