Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:43:22+00:00 2026-05-14T22:43:22+00:00

I’m brand new to Python and trying to learn it by replicating the following

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:43:23+00:00Added an answer on May 14, 2026 at 10:43 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to select an H1 element which is the second-child in its group
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.