954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove words with less than N letters from wordlist

Hey, I just started programming but im stuck. so lets say I have a wordlist with words containing 2-12 letters and I want to remove all words containing less than 3 letters or more than 7 letters. How would i do that?

Another problem I have is, I want to find all words that contain for example these letters "negi" (it should return engine, engineer and alot of other words) , I know how to make it find all words if it is typed "engi" but not if its typed like "negi".

dappe
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

I can help you with first problem.
You have to show some effort,some just post there school task here.
That means that you have to post some code of what you are struggling with.

>>> s = 'a fine day with beautiful sun'
>>> l = s.split()
>>> l
['a', 'fine', 'day', 'with', 'beautiful', 'sun']
>>> [i for i in l if len(i) >= 3 and len(i) <=7]
['fine', 'day', 'with', 'sun']
>>> #Or better
>>> [i for i in l if 3 <=  len(i) <= 7]
['fine', 'day', 'with', 'sun']

Here i use list comprehension this is ofen used in python.
If you dont understand it,try to break it up with a ordinary loop.

snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

For the second problem, you can use re.search, with a fairly simple regex. Something like this should do: [engi]{4}

That will match a lot of words that don't have ALL four of those letters though. You can probably refine it form there if needed.

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

For second one I point you to this other current thread, even there is much better ones, as I want you to have practice to doing it yourself:
http://www.daniweb.com/software-development/python/threads/381869/1644529#post1644529

As I have researched anagrams as training project when learning Python myself, I want to wish you luck!

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: