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

  • Home
  • SEARCH
  • 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 8968903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:31:07+00:00 2026-06-15T17:31:07+00:00

I’m working on creating a word cloud program in Python and I’m getting stuck

  • 0

I’m working on creating a word cloud program in Python and I’m getting stuck on a word replace function. I am trying to replace a set of numbers in an html file (so I’m working with a string) with words from an ordered list. So 000 would be replaced with the first word in the list, 001 with the second, etc.

So below I have it selecting the word to replace w properly but I can’t get it to properly replace the it with the words from the string. Any help is appreciated. Thanks!

def replace_all():  
  text = '000 001 002 003 '
  word = ['foo', 'bar', 'that', 'these']
  for a in word:    
    y = -1
    for w in text:     
      y = y + 1
      x = "00"+str(y)
      w = {x:a}      
      for i, j in w.iteritems():
        text = text.replace(i, j)
  print text      
  • 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-06-15T17:31:08+00:00Added an answer on June 15, 2026 at 5:31 pm

    This is actually a really simple list comprehension:

    >>> text = '000 001 002 003 '
    >>> words = ['foo', 'bar', 'that', 'these']
    >>> [words[int(item)] for item in text.split()]
    ['foo', 'bar', 'that', 'these']
    

    Edit: If you need other values to be left alone, this can be catered for:

    def get(seq, item):
        try:
            return seq[int(item)]
        except ValueError:
            return item
    

    Then simply use something like [get(words, item) for item in text.split()] – naturally, more testing might be required in get() if there will be other numbers in the string that could get accidentally replaced. (End of edit)

    What we do is split the text into the individual numbers, then convert them to integers and use them to index the list you have given to find words.

    As to why your code doesn’t work, the main issue is you are looping over the string, which will give you characters, not words. However, it’s not a great way of solving the task.

    It’s also worth a quick note that when you are looping over values and want indices to go with them, you should use the enumerate() builtin rather than using a counting variable.

    E.g: Instead of:

    y = -1
    for w in text:
        y = y + 1
        ...
    

    Use:

    for y, w in enumerate(text):
        ...
    

    This is much more readable and Pythonic.

    Another thing with your existing code is this:

    w = {x:a}      
    for i, j in w.iteritems():
        text = text.replace(i, j)
    

    Which, if you think about it, simplifies down to:

    text = text.replace(x, a)
    

    You are setting w to be a dictionary of one item, then looping over it, but you know it will only ever contain one item.

    A solution that follows your method more closely would be something like this:

    words_dict = {"{0:03d}".format(index): value for index, value in enumerate(words)}
    for key, value in words_dict.items():
        text = test.replace(key, value)
    

    We create a dictionary from the zero padded number string (using str.format()) to the value, then replace for each item. Note as you are using 2.x, you’ll want dict.iteritems(), and if you are pre-2.7, use the dict() builtin on a generator of tuples as dict comprehensions don’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.