-3

I have a string

a = 2,00,02

each element of this string has datatype str.

I want to convert it to a list such as a = [2, 00, 02] so a is a list and each element of a should have datatype int

what I did is a = [int(each) for each in a] which gave me a = [2,0,2] where a is a list and each in a is an integer. But that's not what I was trying to do. I want a = [2, 00, 02] where middle 0 should be 00 and last 02 should be 02 only and not as 0 and 2. How should I do this?

pyLearner
  • 431
  • 1
  • 4
  • 7
  • 1
    If you want each of them to be an `int` then you cannot expect `'00'` to be stored as `00` in `int` – shaktimaan Aug 26 '14 at 17:40
  • Could you rephrase your needs? `02 == 2`. This is only a matter of output formatting. And by default, Python does not shows leading zeros. Are looking for `"{:02d}".format(...)` ? – Sylvain Leroux Aug 26 '14 at 17:42
  • If want a `list` of `int` type data then you can not have `00` or `02` by the way. – salmanwahed Aug 26 '14 at 17:43
  • @Sylvain yeah.. I was looking for the way to represent it in 2 digits. – pyLearner Aug 26 '14 at 18:08
  • You need to rewrite this question so it makes sense. You can't possibly have a string `a = 2,00,02`, because that's not a string; that's a tuple of 3 ints in Python 2.x, and a syntax error in Python 3.x. Also, each element of a string is a single-character string. So, what do you _actually_ have? Is it 'a = '2,00,02'`? Or `a=('2', '00', '02')`? Or something completely different? – abarnert Aug 26 '14 at 20:05

1 Answers1

1

00 is the same number as 0.

In [59]: 00 == 0
Out[59]: True

But Python will always represent this number as 0, never 00. If you want to preserve 00 then you must use strings:

In [56]: a = '2,00,02'

In [57]: a.split(',')
Out[57]: ['2', '00', '02']

If you want to convert a to a list of ints while also preserving the leading zeros, then you'll need to keep track of two lists:

In [65]: astr = a.split(',')

In [66]: astr
Out[66]: ['2', '00', '02']

In [67]: aint = map(int, astr)

In [68]: aint
Out[68]: [2, 0, 2]

In [69]: zip(astr, aint)
Out[69]: [('2', 2), ('00', 0), ('02', 2)]

Use aint for numeric computations, and astr for string formatting/output.


Note that leading 0's in Python are used to represent octal numbers:

In [62]: 011
Out[62]: 9

since 11 base 8 is 9.

So if the string was a = '2,011,02', then you'll need to clarify if you want the integer values [2,11,2] or [2,011,02] which is equal to [2,9,2].

In [70]: [2, 011, 02] == [2, 9, 2]
Out[70]: True

And if your string was a = '2,09,02', then if you convert each numeric string to an int you'd get [2, 9, 2], but if you want [2, 09, 02] then you'd get a SyntaxError since 09 is not a valid octal number.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • He wants __int__ in list not __string__ and sounds like it is impossible . – Alireza Sanaee Aug 26 '14 at 17:42
  • Ints are not saved with leading zeroes, for the string representation, look at this here: http://stackoverflow.com/a/3371180/3216660 – TidB Aug 26 '14 at 17:43
  • The OP didn't say whether this was Python 2 or Python 3; in Python 3, leading 0's no longer represent octal numbers; they're a syntax error. (To represent octal numbers in both 2.x and 3.x, use `0o11`.) – abarnert Aug 26 '14 at 20:03
  • Meanwhile, the way to convert strings like `'09'` to decimal integers (in both 2.x and 3.x) is to call `int` with an explicit base: `[int(x, 10) for x in astr]`. – abarnert Aug 26 '14 at 20:04