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?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!news.astraweb.com!border5.a.newsrouter.astraweb.com!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <lorenzofsutton@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'subject:Python': 0.05; '[1,': 0.09; 'subject:into': 0.09; 'python': 0.10; '-1):': 0.16; 'desired:': 0.16; 'elements,': 0.16; 'integer.': 0.16; 'interesting:': 0.16; 'pythonic': 0.16; 'wrote:': 0.16; 'string': 0.17; 'integer': 0.18; '>>>': 0.20; 'math': 0.20; '2015': 0.20; 'meant': 0.22; 'sep': 0.22; 'appears': 0.23; 'elements': 0.23; 'second': 0.24; 'import': 0.24; 'previously': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'question': 0.27; '-0700,': 0.29; 'print': 0.30; 'creating': 0.30; 'code': 0.30; "can't": 0.32; 'third': 0.33; 'message- id:@gmail.com': 0.34; 'tue,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'does': 0.39; 'expressed': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'skip:y 20': 0.63; 'intent': 0.66; 'results': 0.66; 'direct': 0.68; '(10': 0.84; 'denis': 0.84; 'respectively': 0.84; 'seventh': 0.84; 'sixth': 0.84; 'ask,': 0.91; 'received:172.16.1': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=reK3hkBpbVwIfagarLdKL7SCohbluz/QysEBhp/0brU=; b=MaXeE+gdhG5TR3Aqi2hN6Q3DW1WKWbH38eIdFZ0qqlt4/fomErg220fgGaoi51L4LO CIzyrpfCpWcrYuQasSkFZpPSViXMKYOlHM8E4Xz0/nbkjCZimkV+x4Xdu5SriIDXNACr Ve3nGYyxO2bZtzs35mMA1tKfnr6bYxIoZSMg9HnoC9CtorOKeyz3gsY8OGd9ITjlz0NQ fM6jlaC6kGPTZTVvq3zGSpBQ1o8RW+U7TDD17XKjmFsv5CMKzr6vYyCzCji/H/M51OQ9 +Krc40hfZC/VRVC/vEiQ1OF74hJoNeInk2AhTGXOOyrkWvVpUdLqvruqfTSunaX1RSPe vVkw==
X-Received by 10.180.91.12 with SMTP id ca12mr11265671wib.4.1443111192581; Thu, 24 Sep 2015 09:13:12 -0700 (PDT)
Subject Re: Python, convert an integer into an index?
To python-list@python.org
References <e6e800a2-a64c-47dc-87b3-c6d32b6fca2f@googlegroups.com> <mtugmv$nav$1@dont-email.me>
From Lorenzo Sutton <lorenzofsutton@gmail.com>
Date Thu, 24 Sep 2015 18:13:14 +0200
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0
MIME-Version 1.0
In-Reply-To <mtugmv$nav$1@dont-email.me>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.134.1443111195.28679.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443111195 news.xs4all.nl 23860 [2001:888:2000:d::a6]:48860
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97080

Show key headers only | 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