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


Groups > comp.lang.python > #96999 > unrolled thread

Python, convert an integer into an index?

Started byChris Roberts <thecjguy1@gmail.com>
First post2015-09-22 14:43 -0700
Last post2015-09-23 10:55 -0500
Articles 13 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  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

#96999 — Python, convert an integer into an index?

FromChris Roberts <thecjguy1@gmail.com>
Date2015-09-22 14:43 -0700
SubjectPython, convert an integer into an index?
Message-ID<e6e800a2-a64c-47dc-87b3-c6d32b6fca2f@googlegroups.com>
(How do I make it into an index? )
Preferably something fairly easy to understand as I am new at this.

results = 134523      #(Integer) 

Desired: 
results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)

Somehow I see ways to convert index to list to int, but not back again.

Thanks, 
crzzy1

[toc] | [next] | [standalone]


#97000

FromLaura Creighton <lac@openend.se>
Date2015-09-23 00:21 +0200
Message-ID<mailman.73.1442960495.28679.python-list@python.org>
In reply to#96999
In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes:
>
>
>(How do I make it into an index? )
>Preferably something fairly easy to understand as I am new at this.
>
>results = 134523      #(Integer) 
>
>Desired: 
>results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
>
>Somehow I see ways to convert index to list to int, but not back again.
>
>Thanks, 
>crzzy1

You need to convert your results into a string first.

result_int=1234523
result_list=[]

for digit in str(result_int):   
    result_list.append(int(digit))

digit will be assigned to successive 1 character long strings.  Since
you wanted a list of integers, you have to convert it back.

If you are learning python you may be interested in the tutor mailing
list. https://mail.python.org/mailman/listinfo/tutor  

Laura

[toc] | [prev] | [next] | [standalone]


#97006

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-09-23 00:23 +0100
Message-ID<mailman.78.1442964260.28679.python-list@python.org>
In reply to#96999
On 22/09/2015 22:43, Chris Roberts wrote:
>
> (How do I make it into an index? )
> Preferably something fairly easy to understand as I am new at this.
>
> results = 134523      #(Integer)
>
> Desired:
> results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
>
> Somehow I see ways to convert index to list to int, but not back again.
>
> Thanks,
> crzzy1
>

Please provide the algorithm to convert 134523 into [1, 2, 3, 4, 5, 2, 
3], as I don't see how to convert a six digit integer into a list of 
seven integers.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#97007

FromMRAB <python@mrabarnett.plus.com>
Date2015-09-23 00:27 +0100
Message-ID<mailman.79.1442964432.28679.python-list@python.org>
In reply to#96999
On 2015-09-22 23:21, Laura Creighton wrote:
> In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes:
>>
>>
>>(How do I make it into an index? )
>>Preferably something fairly easy to understand as I am new at this.
>>
>>results = 134523      #(Integer)
>>
>>Desired:
>>results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
>>
>>Somehow I see ways to convert index to list to int, but not back again.
>>
>>Thanks,
>>crzzy1
>
> You need to convert your results into a string first.
>
> result_int=1234523
> result_list=[]
>
> for digit in str(result_int):
>      result_list.append(int(digit))
>
> digit will be assigned to successive 1 character long strings.  Since
> you wanted a list of integers, you have to convert it back.
>
> If you are learning python you may be interested in the tutor mailing
> list. https://mail.python.org/mailman/listinfo/tutor
>
A shorter way using strings:

>>> results = 134523
>>> list(map(int, str(results)))
[1, 3, 4, 5, 2, 3]

[toc] | [prev] | [next] | [standalone]


#97020

Frommarco.nawijn@colosso.nl
Date2015-09-22 23:55 -0700
Message-ID<819c13d8-670c-46e2-8a43-c8224b42867f@googlegroups.com>
In reply to#97007
On Wednesday, September 23, 2015 at 1:27:51 AM UTC+2, MRAB wrote:
> On 2015-09-22 23:21, Laura Creighton wrote:
> > In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes:
> >>
> >>
> >>(How do I make it into an index? )
> >>Preferably something fairly easy to understand as I am new at this.
> >>
> >>results = 134523      #(Integer)
> >>
> >>Desired:
> >>results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
> >>
> >>Somehow I see ways to convert index to list to int, but not back again.
> >>
> >>Thanks,
> >>crzzy1
> >
> > You need to convert your results into a string first.
> >
> > result_int=1234523
> > result_list=[]
> >
> > for digit in str(result_int):
> >      result_list.append(int(digit))
> >
> > digit will be assigned to successive 1 character long strings.  Since
> > you wanted a list of integers, you have to convert it back.
> >
> > If you are learning python you may be interested in the tutor mailing
> > list. https://mail.python.org/mailman/listinfo/tutor
> >
> A shorter way using strings:
> 
> >>> results = 134523
> >>> list(map(int, str(results)))
> [1, 3, 4, 5, 2, 3]
Or you can use a list comprehension:

>>> result = [int(c) for c in str(134523)]
>>> print result
[1, 3, 4, 5, 2, 3]

[toc] | [prev] | [next] | [standalone]


#97015

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-09-22 23:13 -0400
Message-ID<mailman.87.1442977993.28679.python-list@python.org>
In reply to#96999
On Wed, 23 Sep 2015 00:21:22 +0200, Laura Creighton <lac@openend.se>
declaimed the following:


>
>You need to convert your results into a string first.
>
>result_int=1234523
>result_list=[]
>
>for digit in str(result_int):   
>    result_list.append(int(digit))
>

	Rather wordy... <G>

>>> [int(i) for i in str(1234523)]
[1, 2, 3, 4, 5, 2, 3]

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#97072

FromAnssi Saari <as@sci.fi>
Date2015-09-23 12:01 +0300
Message-ID<vg3pp18x8e6.fsf@coffee.modeemi.fi>
In reply to#97015
Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> On Wed, 23 Sep 2015 00:21:22 +0200, Laura Creighton <lac@openend.se>
> declaimed the following:
>
>
>>
>>You need to convert your results into a string first.
>>
>>result_int=1234523
>>result_list=[]
>>
>>for digit in str(result_int):   
>>    result_list.append(int(digit))
>>
>
> 	Rather wordy... <G>
>
>>>> [int(i) for i in str(1234523)]
> [1, 2, 3, 4, 5, 2, 3]

I'm suprised. Why not just:

list(str(results))

In other words, is there something else the list constructor should do
with a string other than convert it to a list?

[toc] | [prev] | [next] | [standalone]


#97073

FromMRAB <python@mrabarnett.plus.com>
Date2015-09-24 14:30 +0100
Message-ID<mailman.125.1443101432.28679.python-list@python.org>
In reply to#97072
On 2015-09-23 10:01, Anssi Saari wrote:
> Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:
>
>> On Wed, 23 Sep 2015 00:21:22 +0200, Laura Creighton <lac@openend.se>
>> declaimed the following:
>>
>>
>>>
>>>You need to convert your results into a string first.
>>>
>>>result_int=1234523
>>>result_list=[]
>>>
>>>for digit in str(result_int):
>>>    result_list.append(int(digit))
>>>
>>
>> 	Rather wordy... <G>
>>
>>>>> [int(i) for i in str(1234523)]
>> [1, 2, 3, 4, 5, 2, 3]
>
> I'm suprised. Why not just:
>
> list(str(results))
>
> In other words, is there something else the list constructor should do
> with a string other than convert it to a list?
>
The OP wanted the result to be a list of ints, not a list of strings.

[toc] | [prev] | [next] | [standalone]


#97076

Fromjmp <jeanmichel@sequans.com>
Date2015-09-24 16:14 +0200
Message-ID<mailman.128.1443104074.28679.python-list@python.org>
In reply to#97072
On 09/24/2015 03:45 PM, paul.hermeneutic@gmail.com wrote:
>  >> I'm suprised. Why not just:
>  >>
>  >> list(str(results))
>  >>
>  >> In other words, is there something else the list constructor should do
>  >> with a string other than convert it to a list?
>  >>
>  > The OP wanted the result to be a list of ints, not a list of strings.
>
> [int(x) for x in list(str(results))]

Side note : strings are already iterable, the list function is superfluous.


jm

[toc] | [prev] | [next] | [standalone]


#97110

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2015-09-24 21:26 -0400
Message-ID<mailman.158.1443144375.28679.python-list@python.org>
In reply to#97072
On Thu, 24 Sep 2015 07:45:50 -0600, <paul.hermeneutic@gmail.com> declaimed
the following:

>>> I'm suprised. Why not just:
>>>
>>> list(str(results))
>>>
>>> In other words, is there something else the list constructor should do
>>> with a string other than convert it to a list?
>>>
>> The OP wanted the result to be a list of ints, not a list of strings.
>
>[int(x) for x in list(str(results))]

	results is already a string, so why convert it to a string, and list()
is just splitting the string on each character.

[int(x) for x in results]

takes out all the intervening crud.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#97040

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-09-23 15:32 +0000
Message-ID<mtugmv$nav$1@dont-email.me>
In reply to#96999
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

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#97080

FromLorenzo Sutton <lorenzofsutton@gmail.com>
Date2015-09-24 18:13 +0200
Message-ID<mailman.134.1443111195.28679.python-list@python.org>
In reply to#97040

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]

[toc] | [prev] | [next] | [standalone]


#97041

FromTim Daneliuk <tundra@bogus-city.tundraware.com>
Date2015-09-23 10:55 -0500
Message-ID<5kuadc-qg4.ln1@oceanview.tundraware.com>
In reply to#96999
On 09/22/2015 04:43 PM, Chris Roberts wrote:
> 
> (How do I make it into an index? )
> Preferably something fairly easy to understand as I am new at this.
> 
> results = 134523      #(Integer) 
> 
> Desired: 
> results = [1, 2, 3, 4, 5, 2, 3]   #(INDEX)
> 
> Somehow I see ways to convert index to list to int, but not back again.
> 
> Thanks, 
> crzzy1
> 



results = [x for x in str(results)]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web