473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

generators/iterators: filtered random choice

gry
I want a function (or callable something) that returns a random
word meeting a criterion. I can do it like:

def random_richer_word(word):
'''find a word having a superset of the letters of "word"'''
if len(set(word) == 26): raise WordTooRichException, word
while True:
w = random.choice(words)
if set(w) - set(word): # w has letters not present in word
return w

This seems like a perfect application for generators or iterators,
but I can't quite see how. Any suggestions?

Sep 16 '06 #1
1 1500
On 15 Sep 2006 19:17:25 -0700, gr*@ll.mit.edu <gr*@ll.mit.eduwrote:
I want a function (or callable something) that returns a random
word meeting a criterion. I can do it like:

def random_richer_word(word):
'''find a word having a superset of the letters of "word"'''
if len(set(word) == 26): raise WordTooRichException, word
while True:
w = random.choice(words)
if set(w) - set(word): # w has letters not present in word
return w

This seems like a perfect application for generators or iterators,
but I can't quite see how. Any suggestions?
Simply change the return to yield. The function will return a
generator which will iterate over each new word found. I would fix it
up a bit more than that, however. For example, create the set for
word's letters before the loop, and keep a set of previously yielded
words, so you dont use the same one twice.

Alternatively, maybe you meant that you still want to get a single
word, but you want to iterate until its done, so its non-blocking in
nature. In that case, you might still yield, but add an else block to
yield None, and keep iterating until it yields something evaluating
true. In either case I would be pre-computing that set of subset
letters.
Sep 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
7
by: marduk | last post by:
I have a weird request. I want to be able to say def myvalues(): while True: # stuff that determines a new somevalue yield somevalue x = "Hello, %s, this is a %s with %s and %s on top of...
9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be made more efficient. Right now if you do use a...
5
by: Robert Oschler | last post by:
Preamble: - I know this is the Python forum - I know about (and have used) Jython I already posted this question in comp.lang.java. But after a week I have still not received a single reply....
10
by: Brian Roberts | last post by:
I'm using using generators and iterators more and more intead of passing lists around, and prefer them. However, I'm not clear on the best way to detect an empty generator (one that will return no...
4
by: pmatos | last post by:
Hi all, How can I compute the size of vector<int> given iterators to it. Can I do : int size = itEnd - itBegin; ??? Cheers, Paulo Matos
3
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
3
by: James J. Besemer | last post by:
I would like to champion a proposed enhancement to Python. I describe the basic idea below, in order to gage community interest. Right now, it's only an idea, and I'm sure there's room for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.