473,503 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newbie: for loop within for loop question

Hi everyone,

I'm studying python via the excellent "how to think like a python
programmer" book by Allen Downey. Noob question follows...

I have a txt file (animals.txt) which contains the following text each
on a separate line: aardvark, bat, cat, dog, elephant, fish, giraffe,
horse, inchworm, jackelope

I want to create a function that loops through the animals.txt file
and does *not* print the word if any of the user specified letters are
in that word.

def hasnolet(x):

Jun 27 '08 #1
2 964
Dennis,

thanks for your reply. unfortunately i accidentally posted only half
of my question! the "real" post should be up now.

my apologies.

takayuki
On Jun 16, 10:15 am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
<lawtonp...@gmail.comdeclaimed the following in comp.lang.python:
Hi everyone,
I'm studying python via the excellent "how to think like a python
programmer" book by Allen Downey. Noob question follows...
I have a txt file (animals.txt) which contains the following text each
on a separate line: aardvark, bat, cat, dog, elephant, fish, giraffe,
horse, inchworm, jackelope
I want to create a function that loops through the animals.txt file
and does *not* print the word if any of the user specified letters are
in that word.
def hasnolet(x):

Hope this wasn't a homework assignment... It gave me my first excuse
to try the set module...
>import sets
words = [ "aardvark", "bat", "cat", "dog", "elephant", "fish" ]
exclude = "want"
excludeset = sets.Set(exclude)
excludeset

Set(['a', 't', 'w', 'n'])>>for w in words:

... if not excludeset.intersection(w):
... print w
...
dog
fish

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-a...@bestiaria.com)
HTTP://www.bestiaria.com/
Jun 27 '08 #2
The sets module is no longer needed, as we have the built-in sets
type. Its even getting a literal syntax soon.

As for the original problem, I agree on the homework smell.

On Jun 15, 2008, at 9:31 PM, takayuki wrote:
Dennis,

thanks for your reply. unfortunately i accidentally posted only half
of my question! the "real" post should be up now.

my apologies.

takayuki
On Jun 16, 10:15 am, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
>On Sun, 15 Jun 2008 17:18:54 -0700 (PDT), takayuki
<lawtonp...@gmail.comdeclaimed the following in comp.lang.python:
>>Hi everyone,
>>I'm studying python via the excellent "how to think like a python
programmer" book by Allen Downey. Noob question follows...
>>I have a txt file (animals.txt) which contains the following text
each
on a separate line: aardvark, bat, cat, dog, elephant, fish,
giraffe,
horse, inchworm, jackelope
>>I want to create a function that loops through the animals.txt file
and does *not* print the word if any of the user specified
letters are
in that word.
>>def hasnolet(x):

Hope this wasn't a homework assignment... It gave me my
first excuse
to try the set module...
>>>>import sets
words = [ "aardvark", "bat", "cat", "dog", "elephant", "fish" ]
exclude = "want"
excludeset = sets.Set(exclude)
excludeset

Set(['a', 't', 'w', 'n'])>>for w in words:

... if not excludeset.intersection(w):
... print w
...
dog
fish

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfr...@ix.netcom.com wulfr...@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-
a...@bestiaria.com)
HTTP://www.bestiaria.com/

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

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

Similar topics

3
2656
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
16
3490
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
3937
by: LostDeveloper via AccessMonster.com | last post by:
Relationship goes Certification can have multiple variations (any single variation satifies the certification), which require multiple course groups which require any one of the products assigned...
2
1247
pdring
by: pdring | last post by:
Hi guys, sorry but yet another newbie struggling with the basics! I have a small program that i am working on in wich 1 want to enter exam scores for each of 5 students in turn untill i enter a set...
17
1460
by: takayuki | last post by:
Hi, I'm studying python via the exellent book "How to think like a python programmer" by Allen Downey. Noob question follows... animals.txt is a list of animals, each on a separate line:...
7
1502
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
12
1815
by: Philipp.Weissenbacher | last post by:
Hi all! This is most certainly a total newbie question, but why doesn't the following code cause a segfault? void insertion_sort(int a, int length) { int i; for (i=0; i < length; i++) { int...
5
1390
by: Dave | last post by:
I am new to Visual Web Developer 2005 Expres. I am using absolute positioning and every time I add a button control to my web form its width extends all the way to the edge of the page. IOW I...
0
2715
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.