views:

455

answers:

4

What is the reason that you cannot use zero at the beginning of a number when converting the number to a sequence?

Code example

map(int,str(08978789787))

which gives Syntax error.

I would like to convert numbers which leading digit is zero to a sequence. How can you convert such a number to a sequence?

+4  A: 

http://stackoverflow.com/questions/336181/python-invalid-token

Use:

map(int,"08978789787")
liori
+1: leading zero is octal. Octal can't include digits 8 or 9.
S.Lott
+8  A: 

What's because the leading zero means you are writing an octal number and you can't have 9 or 8 in an octal number. See:

>>> a = 0123
>>> a
83
>>> a = 010
>>> a
8

You can just do:

>>> map(int, '08978789787')
[0, 8, 9, 7, 8, 7, 8, 9, 7, 8, 7]
Nadia Alramli
+7  A: 

The "leading 0 in an integer means it's in octal notation" meme is a peculiar one which originated in C and spread all over the place -- Python (1.* and 2.*), Perl, Ruby, Java... Python 3 has eliminated it by making a leading 0 illegal in all integers (except in the constructs 0x, 0b, 0o to indicate hex, binary and octal notations).

Nevertheless, even in a hypothetical sensible language where a leading 0 in an int had its normal arithmetical meaning, that is, no meaning whatsoever, you still would not obtain your desired result: 011 would then be exactly identical to 11, so calling str on either of them would have to produce identical results -- a string of length two, '11'.

In arithmetic, the integer denoted by decimal notation 011 is identical, exactly the same entity as, indistinguishable from, one and the same with, the integer denoted by decimal notation 11. No hypothetical sensible language would completely alter the rules of arithmetic, as would be needed to allow you to obtain the result you appear to desire.

So, like everybody else said, just use a string directly -- why not, after all?!

Alex Martelli
Because he is trying to understand how sum(map(int, str(i)) works for an integer. He has another question about that. Anyway...
Lennart Regebro
IOW - the representation of the number (the sequence of digit characters in the source code, or the sequence of digit characters in the output) is not the same thing as the abstract number, or as the internal representation used by the language. Python (like most languages) has an internal representation using binary, in chunks designed to suit machine registers, and doesn't keep information such as how many leading zeros in a decimal representation.
Steve314
@Steve314, excellent alternative viewpoint/presentation.
Alex Martelli
+1  A: 

"How can you convert such a number to a sequence?"

There is no "such a number". The number 1 does not start with a 0. Numbers do not start with zeros in general (if they did, you would need to write an infinite amount of zeros every time you write a number, and that would obviously be impossible).

So the question boils down to why you are writing str(08978789787)? If you want the string '08978789787', you should reasonably just write the string '08978789787'. Writing it as a number and the converting it to a string is completely pointless.

Lennart Regebro
You say "there is no such number", but the digits are parts of representations, not of the number itself. The number is an abstraction. You will never see the number 11 in the real world, though you may see 11 apples or 11 oranges or the decimal representations "11" or "00000011", or the binary representation "1011" or the word "eleven", or the symbols "XI" etc. It's like the word "dog" is not the same as the dog itself, though at least the actual dog is real, unlike numbers - depending on what you mean by *real* number, of course ;-)
Steve314
Yes. And the representation "11" is not a different number from "011", it's the number eleven in both cases. So there is no number 011, just a number eleven. The idea therefore to ask how you can use the number zeroeleven, makes no sense, and that what he asked for.
Lennart Regebro
I didn't say you're wrong - just commenting on your explanation. My point is that you don't get to claim 011 doesn't exist just because it isn't your favorite representation. There's no one true representation. Leading zeros are redundant, but *not* invalid. In Python numeric literals it's different - the representation with a leading zero has a different meaning. Even so, 01 and 011 etc still exist, it's just that they represent the same value as decimal 1 and 9 respectively.
Steve314
My point is that 011 is not a number, it's a representation, and that the number zeroeleven doesn't exist. But that's what he is asking for. It' spretty obvious that he does not understand the finer details of python number representation, and thing that 011 is the same as 11. I point out that zeroeleven isn't a number.And it sounds to me, once again, that you *are* saying that I'm wrong. :)
Lennart Regebro
Except that you also said exactly what I said in a comment to Alex Martelli. Ah well. :)
Lennart Regebro
Hint - the accurate way to criticise me would be to call me ridiculously over-pedantic WRT the representation vs. number distinction. Of course, that might be an overly pedantic approach to the criticism too ;-)
Steve314