Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63543
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Dictionary |
| Date | 2014-01-08 19:13 -0500 |
| Organization | IISS Elusive Unicorn |
| References | <x4jwu.3413$Bs5.1921@fx09.am4> <mailman.4725.1388433616.18130.python-list@python.org> <P6hzu.2028$Wn7.1602@fx20.am4> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5214.1389226408.18130.python-list@python.org> (permalink) |
On Wed, 08 Jan 2014 18:51:26 +0000, Bischoop <martin@jakis.adres.em>
declaimed the following:
>Dennis Lee Bieber wrote:
>
>> On Mon, 30 Dec 2013 18:38:20 +0000, Bischoop <martin@jakis.adres.em>
>> declaimed the following:
>>
>>>I have a txt file with some words, and need simply program that will
>>>print me words containing provided letters.
>>>
>>>For example:
>>>Type the letters:
>>> (I type: g,m,o)
>>>open the dictionary.txt
>>>check words containing:g,m,o in dictionary.txt
>>>if there are words containing: ["g", "m", "o" ]
>>>print words with g,m,o
>>
>> Vague requirement...
>>
>> Do you need:
>> 1 any word containing any single letter
>> 2 any word containing all supplied letters (is there a limit on how
>many
>> letters?)
>> 3 any word containing the supplied letters in the entered order,
>but not
>> necessarily adjacent to each other
>> 4 any word containing the supplied letters in adjacent sequence
>> 5 any word that begins with the supplied sequence of letters
>
>1.yes
>2. Any combination with supplied letters. It would be great if possible that
>I could set also the combination for example: show me all words with: l,x
>3. the order doesn't matter
>4. doesnt matter
>5 doesnt matter.
No doubt someone will suggest using a regular expression, but since
I've never used the re/regex modules I can't give the full work...
PseudoCode may be something like
fwin = open("dictionary.txt", "r")
haystack = fwin.read().lower().split("\n")
fwin.close()
while True:
letters = input("Enter letters> ")
if not letters: break
#generate search re expression representing
# .* any character/multiple -- leading
# [l|e|t|t|e|r] match any of the letters supplied
# .* any character/multiple -- trailing
needle = ".*[" + "|".join(list(letters.lower())) + "].*"
#do a regular expression search of haystack using needle as
#search condition
# print matches
----
The slower non-re version might be something like
fwin = open("dictionary.text", "r")
haystack = fwin.read().lower().split("\n")
fwin.close()
while True:
letters = input("Enter letters> ").lower()
if not letters: break
for word in haystack:
for letter in letters:
if letter in word:
print word
break
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dictionary Bischoop <martin@jakis.adres.em> - 2013-12-30 18:38 +0000
Re: Dictionary Walter Hurry <walterhurry@gmail.com> - 2013-12-30 18:56 +0000
Re: Dictionary Bischoop <martin@jakis.adres.em> - 2014-01-08 19:00 +0000
Re: Dictionary wxjmfauth@gmail.com - 2014-01-09 00:31 -0800
Re: Dictionary Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-30 15:00 -0500
Re: Dictionary Bischoop <martin@jakis.adres.em> - 2014-01-08 18:51 +0000
Re: Dictionary Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-08 19:13 -0500
Re: Dictionary Chris Angelico <rosuav@gmail.com> - 2014-01-09 11:21 +1100
csiph-web