Itertools question: how to call a function n times?

M

Matthew Wilson

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
 
B

Bruno Desthuilliers

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)]
 
W

Wojciech =?iso-8859-2?Q?Mu=B3a?=

Matthew said:
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.
 
S

Stargaming

Matthew said:
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
 
J

jrbj

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
 

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,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top