Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74951 > unrolled thread

How to extract digit from a number?

Started byfl <rxjwg98@gmail.com>
First post2014-07-21 13:14 -0700
Last post2014-07-21 15:57 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  How to extract digit from a number? fl <rxjwg98@gmail.com> - 2014-07-21 13:14 -0700
    Re: How to extract digit from a number? Tim Chase <python.list@tim.thechases.com> - 2014-07-21 15:26 -0500
      Re: How to extract digit from a number? fl <rxjwg98@gmail.com> - 2014-07-21 13:42 -0700
        Re: How to extract digit from a number? Roy Smith <roy@panix.com> - 2014-07-21 16:53 -0400
        Re: How to extract digit from a number? Tim Chase <python.list@tim.thechases.com> - 2014-07-21 15:57 -0500

#74951 — How to extract digit from a number?

Fromfl <rxjwg98@gmail.com>
Date2014-07-21 13:14 -0700
SubjectHow to extract digit from a number?
Message-ID<12e1f726-c083-49db-86bd-1c9ae3151b2a@googlegroups.com>
Hi,

I see the following example on line, but it does not work. I do not know what is
wrong. Could you correct it for me?


Thanks,



........................
I'm not sure what [1, 1, 0, 0, 0, 0, ...] has to do with 128, but if you want the 
base 10 digits:

>>> a = 1234
>>> [int(d) for d in str(a)]
>>> [1, 2, 3, 4]

[toc] | [next] | [standalone]


#74952

FromTim Chase <python.list@tim.thechases.com>
Date2014-07-21 15:26 -0500
Message-ID<mailman.12151.1405974457.18130.python-list@python.org>
In reply to#74951
On 2014-07-21 13:14, fl wrote:
> I see the following example on line, but it does not work. I do not
> know what is wrong. Could you correct it for me?
> 
> I'm not sure what [1, 1, 0, 0, 0, 0, ...] has to do with 128, but
> if you want the base 10 digits:
> 
> >>> a = 1234
> >>> [int(d) for d in str(a)]
> >>> [1, 2, 3, 4]

You don't specify *what* is wrong or what constitutes "does not
work".  If you provide an example of what you *do* want, folks here
can help you get closer to the code you need to do what you intend.

-tkc

[toc] | [prev] | [next] | [standalone]


#74953

Fromfl <rxjwg98@gmail.com>
Date2014-07-21 13:42 -0700
Message-ID<d84aa18f-7c12-4262-a746-218581783304@googlegroups.com>
In reply to#74952
On Monday, July 21, 2014 4:26:25 PM UTC-4, Tim Chase wrote:
> On 2014-07-21 13:14, fl wrote:
> You don't specify *what* is wrong or what constitutes "does not
> work".  If you provide an example of what you *do* want, folks here
> can help you get closer to the code you need to do what you intend.
> 
> -tkc

The original source input is:
>>> a = 1234
>>> [int(d) for d in str(a)] 

He hopes the output is:
>>> [1, 2, 3, 4] 

In fact, I get the output is:

>>> a = 1234
>>> [int(d) for d in str(a)] 
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'str' object is not callable

BTW, I just add input:
>>> import string

The error is still there. 
Why does it say :"TypeError: 'str' object is not callable"?

[toc] | [prev] | [next] | [standalone]


#74955

FromRoy Smith <roy@panix.com>
Date2014-07-21 16:53 -0400
Message-ID<roy-E6375D.16531021072014@news.panix.com>
In reply to#74953
In article <d84aa18f-7c12-4262-a746-218581783304@googlegroups.com>,
 fl <rxjwg98@gmail.com> wrote:

> >>> a = 1234
> >>> [int(d) for d in str(a)] 
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in <module>
> TypeError: 'str' object is not callable

This looks like you've overwritten str with an *instance* of a string.

When a python interpreter starts up, there are certain pre-defined 
symbols.  For example, 'str' is bound to the string class.  But, there's 
nothing to prevent you from re-defining it.  Here's an example which 
produces the same result you got:

---------------------------------------------------------------------
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on 
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> str = "foo"
>>> a = 1234
>>> [int(d) for d in str(a)] 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
---------------------------------------------------------------------

[toc] | [prev] | [next] | [standalone]


#74956

FromTim Chase <python.list@tim.thechases.com>
Date2014-07-21 15:57 -0500
Message-ID<mailman.12152.1405976291.18130.python-list@python.org>
In reply to#74953
On 2014-07-21 13:42, fl wrote:
> The original source input is:
> >>> a = 1234
> >>> [int(d) for d in str(a)] 
> 
> He hopes the output is:
> >>> [1, 2, 3, 4] 
> 
> In fact, I get the output is:
> 
> >>> a = 1234
> >>> [int(d) for d in str(a)] 
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in <module>
> TypeError: 'str' object is not callable

This sounds suspiciously like you have shadowed the str() function.
You might search your code for something like

  str = "test"

which shadows the built-in str() function.  The code you have works at
the command-line as long as you haven't shadowed the
previously-existing str() function:

$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1234
>>> [int(d) for d in str(a)]
[1, 2, 3, 4]

-tkc


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web