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: 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: From: Lorenzo Sutton 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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]