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


Groups > comp.lang.python > #97080

Re: Python, convert an integer into an index?

Subject Re: Python, convert an integer into an index?
References <e6e800a2-a64c-47dc-87b3-c6d32b6fca2f@googlegroups.com> <mtugmv$nav$1@dont-email.me>
From Lorenzo Sutton <lorenzofsutton@gmail.com>
Date 2015-09-24 18:13 +0200
Newsgroups comp.lang.python
Message-ID <mailman.134.1443111195.28679.python-list@python.org> (permalink)

Show all headers | View raw



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
> digits
>
>> Desired:
>> 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):

 >>> 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))
	y.append(int(number))

 >>> y
[1, 2, 3, 4, 5, 6, 7]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python, convert an integer into an index? Chris Roberts <thecjguy1@gmail.com> - 2015-09-22 14:43 -0700
  Re: Python, convert an integer into an index? Laura Creighton <lac@openend.se> - 2015-09-23 00:21 +0200
  Re: Python, convert an integer into an index? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-23 00:23 +0100
  Re: Python, convert an integer into an index? MRAB <python@mrabarnett.plus.com> - 2015-09-23 00:27 +0100
    Re: Python, convert an integer into an index? marco.nawijn@colosso.nl - 2015-09-22 23:55 -0700
  Re: Python, convert an integer into an index? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-22 23:13 -0400
    Re: Python, convert an integer into an index? Anssi Saari <as@sci.fi> - 2015-09-23 12:01 +0300
      Re: Python, convert an integer into an index? MRAB <python@mrabarnett.plus.com> - 2015-09-24 14:30 +0100
      Re: Python, convert an integer into an index? jmp <jeanmichel@sequans.com> - 2015-09-24 16:14 +0200
      Re: Python, convert an integer into an index? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-09-24 21:26 -0400
  Re: Python, convert an integer into an index? Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-23 15:32 +0000
    Re: Python, convert an integer into an index? Lorenzo Sutton <lorenzofsutton@gmail.com> - 2015-09-24 18:13 +0200
  Re: Python, convert an integer into an index? Tim Daneliuk <tundra@bogus-city.tundraware.com> - 2015-09-23 10:55 -0500

csiph-web