Problem with list.insert

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Thu Aug 28 14:13:06 EDT 2008


Subhabrata, it's very difficult for me to understand what your short
program has to do, or what you say. I think that formatting and code
style are important.

So I suggest you to give meaningful names to all your variable names,
to remove unused variables (like n), to add blank likes here and there
to separate logically separated parts of your program, or even better
to split it into functions. You can remove some intermediate function,
coalescing few logically related operations into a line, you can put
spaces around operators like = and after a commas, you can show an
usage example in English, so people can understand what the program is
supposed to to, you can avoid joining and then splitting strings
again, remove useless () around certain things.

This is a possible re-write of the first part of your code, it's not
exactly equal...


def input_words():
    input_message = "Print one English sentence for dictionary check:
"
    return raw_input(input_message).lower().split()


def load_dictionary():
    return set(line.rstrip() for line in open("words.txt"))


def dictionary_search(dictionary, words):
    found = []
    not_found = []

    for word in words:
        if word in dictionary:
            found.append(word)
        else:
            not_found.append(word)

    return found + not_found


inwords = input_words()
dictionary = load_dictionary()
print dictionary_search(dictionary, inwords)


It's far from perfect, but you can use it as starting point for a
rewrite of your whole program.

Bye,
bearophile



More information about the Python-list mailing list