Python, convert an integer into an index?
lorenzofsutton at gmail.com
Thu Sep 24 18:13:14 CEST 2015
On 23/09/2015 17:32, Denis McMahon wrote:
> On Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts wrote:
>> results = 134523 #(Integer)
> This appears to be an integer expressed (presumably) in base 10 with 6
>> results = [1, 2, 3, 4, 5, 2, 3] #(INDEX)
> This appears to be a python list of 7 elements, with the first and the
> the third through seventh elements corresponding to the first and the
> second through sixth most significant digits respectively of the
> previously discussed integer.
> I can't actually see any direct method of creating the list given from
> the number given.
> However, if I understand the intent of the question you meant to ask, you
> might find that the following code does something interesting:
> x = 9876543210
> y = 
> while x > 0:
> y.append(x % 10)
> x = int(x / 10)
> y = list(reversed(y))
> print y
I like the math approach even if the pythonic list string is quicker...
One 'math' way would also be (avoiding the list reverse, but need to
>>> import math
>>> result = 1234567
>>> digits = int(math.log10(result) + 1)
>>> y = 
>>> for x in range(digits, 0, -1):
number = result % (10 ** x) / (10 **(x-1))
[1, 2, 3, 4, 5, 6, 7]
More information about the Python-list