Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to think in Python after working in C++?

Tags:

c++

python

I'm brand new to Python and trying to learn it by replicating the following C++ function into python

// determines which words in a vector consist of the same letters
// outputs the words with the same letters on the same line
void equivalentWords(vector <string> words, ofstream & outFile) {
    outFile << "Equivalent words\n";

    // checkedWord is parallel to the words vector. It is
    // used to make sure each word is only displayed once.
    vector <bool> checkedWord (words.size(), false);

    for(int i = 0; i < words.size(); i++) {
        if (!checkedWord[i]){
            outFile << "  ";
            for(int j = i; j < words.size(); j++){
                if(equivalentWords(words[i], words[j], outFile)) {
                    outFile << words[j] << " ";
                    checkedWord[j] = true;
                }
            }
            outFile << "\n";    
        }   
    }
}

In my python code (below), rather than having a second vector, I have a list ("words") of lists of a string, a sorted list of the chars in the former string (because strings are immutable), and a bool (that tells if the word has been checked yet). However, I can't figure out how to change a value as you iterate through a list.

    for word, s_word, checked in words:
    if not checked:
        for word1, s_word1, checked1 in words:
            if s_word1 == s_word:
                checked1 = True # this doesn't work
                print word1,
        print ""

Any help on doing this or thinking more "Pythony" is appreciated.

like image 701
Josh Avatar asked May 21 '10 01:05

Josh


1 Answers

Keeping things simple, this is O(N) complexity and should be sufficient if you don't have GBs of word data. Note that set() and dict() basically is a hashed index (free and builtin!).

index = {}
for word, s_word in words:
    index[s_word] = index.get(s_word, []) + [word]

for similar_words in index.values():
    print ' '.join(similar_words)        

Don't know what you are using it for, but it might be of interest to you that in python 2.7 a Counter class was introduced in the collections module.

If you really want to keep your algorithm and update a boolean list (which you don't because that algorithm would do inefficient double loops), you would do it like this:

checked = [False] * len(words)
for i, (word, word_s) in enumerate(words):
    if checked[i]:
       continue
    for j, (other, other_s) in enumerate(words[i:]):
        if word_s == other_s:
            print other,
            checked[i+j] = True
    print
like image 148
catchmeifyoutry Avatar answered Sep 23 '22 11:09

catchmeifyoutry