SereneVirus
SereneVirus

Reputation: 1

Matching input letters with a dictionary in Python

I'm trying to make a program that will read in words from a .txt file and having the user input letters of own choosing, and the program will give print out all the matches.

This is what I got so far:

fil = open("example.txt", "r")
words = fil.readlines()
letters = raw_input("Type in letters: ")
compare = set(letters)

lista = []
for a_line in words:
    a_line = a_line.strip()
    lineword = set(a_line)
    if compare >= lineword:
        lista.append(rad)


print lista

Now this works only to a certain degree. It does match the user input with the content of the .txt file, but I want it to be more precise. For example: If I put in "hrose" it will find me "horse", but it will also find me "roses" with two s, since it only compares elements and not amount

How can I make the program to only use the specified letters?

Upvotes: 0

Views: 979

Answers (4)

Chris Clarke
Chris Clarke

Reputation: 2181

Counters are your friend

from collections import Counter

fil = open("example.txt", "r")
words = [(a.strip(), Counter(a.strip())) for a in fil.readlines()]

letters = raw_input("Type in letters: ")
letter_count = Counter(letters)

word_list = []
for word, word_count in words:
    if all([letter_count[char] >= word_count[char] for char in word]):
        word_list.append(word)

print word_list

looking at the comments, it's possible you may only want exact matches, if so, you don't even need a counter

fil = open("example.txt", "r")
words = [(a.strip(), sorted(a.strip())) for a in fil.readlines()]

letters = sorted(raw_input("Type in letters: "))

word_list = [word for word, sorted_word in words if letters == sorted_word]

print word_list

Upvotes: 1

Paulo Almeida
Paulo Almeida

Reputation: 8061

You can use Counter:

from collections import Counter

def compare(query, word):
    query_count = Counter(query)
    word_count = Counter(word)
    return all([query_count[char] >= word_count[char] for char in word])

>>> compare("hrose", "rose")
True
>>> compare("hrose", "roses")
False

Upvotes: 1

venpa
venpa

Reputation: 4318

one approach you could follow is to use set fundtions:

either use issubset/issuperset

set("horse").issubset(set("hrose")) #returs True
set("horse").issubset(set("roses")) #returns False

or

set("horse").difference(set("hrose")) #returns empty set based on set length you know close call
set("horse").difference(set("roses")) #returns set(['h']) 

In the second approach, if you have the choice to choose among multiple options, you could go for result with small length.

Upvotes: 0

Peeyush
Peeyush

Reputation: 726

you can map a mapping dictionary with key as the letters in the word and value being how many times it occurs in that word. Now just compare two dictionaries.

fil = open("example.txt", "r")
words = fil.readlines()
letters = raw_input("Type in letters: ")
compare = list(letters)
letter_dict = {}
for letter in compare:
    try:
        letter_dict[letter] += 1
    except KeyError:
        letter_dict[letter] = 0

lista = []
for a_line in words:
    a_line = a_line.strip()
    lineword = list(a_line)
    word_dict = {}
    for letter in lineword:
        try:
            word_dict[letter] += 1
        except KeyError:
            word_dict[letter] = 0
    flag = True
    for key, value in letter_dict.items():
        if key not in word_dict or word_dict[key] < value:
           flag = False 
           break;
    if flag:
           lista.append(a_line)  

print lista

Upvotes: 0

Related Questions