Re: Python, convert an integer into an index?

2015-09-24 Thread Anssi Saari
Dennis Lee Bieber  writes:

> On Wed, 23 Sep 2015 00:21:22 +0200, Laura Creighton 
> 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... 
>
 [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?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-24 Thread MRAB

On 2015-09-23 10:01, Anssi Saari wrote:

Dennis Lee Bieber  writes:


On Wed, 23 Sep 2015 00:21:22 +0200, Laura Creighton 
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... 


[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.

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-24 Thread paul.hermeneutic
>> 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))]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-24 Thread jmp

On 09/24/2015 03:45 PM, paul.hermeneu...@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


--
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-24 Thread paul.hermeneutic
Good idea.

>>> [int(x) for x in str(results)]
[1, 2, 3]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-24 Thread Lorenzo Sutton



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]
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread Denis McMahon
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, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread Chris Roberts
Fantastic!
I started a course, but I have been having issues with
string/index/list/integer conversions and manipulations.
This variation wasn't in any of my texts or class exercises either.
Your way was both simple enough to understand and very informative!
Thanks.


On Tue, Sep 22, 2015 at 6:21 PM, 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
>
> Laura
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread Tim Daneliuk
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)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-23 Thread marco . nawijn
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]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-22 Thread Ian Kelly
On Sep 22, 2015 3:46 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.

I'm not sure what you mean by "index" in this context, but do you just want
to convert the integer into a list of its digits? The simple way is to
convert it to a string, then convert each character back to an int, and put
the results into a list. More efficient way would be to strip the digits
off one at a time by dividing modulo 10.
-- 
https://mail.python.org/mailman/listinfo/python-list


Python, convert an integer into an index?

2015-09-22 Thread Chris Roberts

(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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-22 Thread Mark Lawrence

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

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-22 Thread Laura Creighton
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


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python, convert an integer into an index?

2015-09-22 Thread MRAB

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]

--
https://mail.python.org/mailman/listinfo/python-list