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 8894559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:31:45+00:00 2026-06-14T23:31:45+00:00

I was answering this question , to which my solution was the following (I

  • 0

I was answering this question, to which my solution was the following (I didn’t post this answer):

def wordigit(w):
    digits = {}
    c = itertools.count(1)
    for char in w:
        if w not in digits:
            digits[char] = c.next()
    return ''.join(str(i) for i in (digits[char] for char in w))

print ' '.join(wordigit(w) for w in s.split())

At first glance, this looks right. But I got an erroneous result:

In [236]: ' '.join(wordigit(w) for w in s.split())
Out[236]: '12345 14345 33345' # notice the numbers in the second "word". 1-4 is not a legal progression for itertools.count. It should have been 12324 instead of 14345

I had developed this solution in IPython. Thinking that this might be a bug with some optimization that IPython does under the hood, I fired up a python interpreter to see what it said. I also thought that the list-comprehension might be leading to some anomalous behaviour. So I tried calling wordigit twice – once on each of two unequal strings and got the following results:

>>> def wordigit(w):
...     digits = {}
...     c = itertools.count(1)
...     for char in w:
...         if w not in digits:
...             digits[char] = c.next()
...     return ''.join(str(i) for i in (digits[char] for char in w))
... 
>>> wordigit('areyo')
'12345'
>>> wordigit('uanap')
'14345'

So it seems that even though I create the itertools.count object within the function (and therefore the function should recreate it at every run), it somehow persists after the function has returned and gets reused without re-initialization when the function is called again. Similarly, It is clear that this is the case for the dict “digits” in wordigit as well.

How is this happening? Somehow, this behavior doesn’t make sense to me.

I notice this behavior with Python 2.7.1 in Ipython and a fresh Python 2.7.3 interpreter

  • 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-14T23:31:46+00:00Added an answer on June 14, 2026 at 11:31 pm

    This doesn’t look right:

    for char in w:
        if w not in digits:
          ^^^
    

    Maybe:

    for char in w:
        if char not in digits: 
           ^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When answering this question I made some research which really confuses me. I noticed
In looking at this question which Jon did a fine job in answering... '
While answering this question I found some strange behavior for which I have no
In answering this question , it got me thinking... I often use this pattern:
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output
I'm asking and answering this question so that I can find it again in
Please bear in mind that I'm totally new to Rails when answering this.My question
In answering this code golf question , I ran across a problem in my
This is all I need to finish answering my last question . If you
Sorry this is a basic question, but all my research just barely missed answering

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.