0

Example:

My Input is:

['00','01', '02', '20', '21']

I have tried:

[int(i) for i in Input]

Which results in:

[0, 1, 2, 20, 21]

But I'd like the output to be:

[00, 01, 02, 20, 21]

If I format using:

['% 02d'% i for i in Input]

To get all the decimal places, my list becomes string being, I'd like the results list to contain integers

Jack Evans
  • 1,697
  • 3
  • 17
  • 33
  • 4
    this is imposible, a number is a number, you are playing with its string representation, you need one or another but you cant have both. – Netwave Jun 28 '17 at 09:15
  • Your need is ill-defined. An integer has no leading 0, that's how it is. It doesn't make sense from a mathematical point of view to insist on it. This is an [xy problem](http://xyproblem.info/). Please tell us what you actually want to do with this list - show some code, and tell us what the final output needs to be. – kabanus Jun 28 '17 at 09:16
  • Please refer to [this](https://stackoverflow.com/a/13499182/7662085) answer. – stelioslogothetis Jun 28 '17 at 09:16
  • Numbers starting with '0' is octal in python. – Arunesh Singh Jun 28 '17 at 09:16
  • I agree with @DanielSanchez, if integer needed, will get only 0,1,2,3; else you need to define your own integer class type which can return values in this way, but python will not have such number format. – Hara Jun 28 '17 at 09:17
  • @AruneshSingh You mean 0x, and the output still won't show it. – kabanus Jun 28 '17 at 09:17
  • 2
    OMG who upvoted this heresy? Whooo?! XD – daveoncode Jun 28 '17 at 09:21
  • @caroline-d-p-n-barbieri; If you can explain your context, as why you need 01,02,... you may get better solution i guess...? – Hara Jun 28 '17 at 09:25
  • I need all the positions because I'm working with ternary logic.This logic is similar to binary, where all bits are required. – Caroline D.P.N. Barbieri Jun 28 '17 at 09:30
  • @CarolineD.P.N.Barbieri You should've included it in the question. Could you parse them as 3-base numbers (`int('10', base=3)`), and process them not minding internal representation? You have to write some code to convert them back to `str` though. – bakatrouble Jun 28 '17 at 09:41

3 Answers3

4

Integers in python cannot have leading zeros. You can store those numbers as str converting them to int as you need those as integers.

Looks like OP wants to work with 3-base numbers, these could be converted from str to int like this:

>>> print(int('10', base=3))
3

Just for information:

(I won't delete this section as there're comments related to it)

Number literals with leading zero are considered 8-base numbers:

>>> print(013)
11
bakatrouble
  • 1,746
  • 13
  • 19
  • but above example list having 20,21 as sample input. so is it, should be considered as base 10 value? and 01, 02, are should be considered 8-base values? – Hara Jun 28 '17 at 09:22
  • @Haranadh I've added 8-base example just for additional information, it is not applicable in this case – bakatrouble Jun 28 '17 at 09:24
  • but the expectation was different, owner of question wants as integer, for 01, 02, 20 ,21 all values. to be converted in same way. – Hara Jun 28 '17 at 09:27
1

You cannot store integer values with leading zeros:

>>> x = 005
  File "<stdin>", line 1
    x = 005
          ^
SyntaxError: invalid token
>>> x = 5

In Python, like in most programming language, integer values are used to store a number, but not to define how this number will be displayed later. Leading zero are used for printing purpose only.

Antwane
  • 20,760
  • 7
  • 51
  • 84
0

If your expectation is to do operations on digits, then you are looking for similar to following:

get_tens = lambda val: int(int(val)/10)
get_100s = lambda val: int(int(val)/100)

def main():
    vals = ['00', '01', '21', '32', '03']
    for i in vals:
        print get_tens(i)
        # do the operation you want on returned value instead of printing.

        print get_100s(i)
        # do the operation you want on returned value instead of printing.

if __name__ == "__main__":
    main()

See If this can help!

Hara
  • 1,467
  • 4
  • 18
  • 35