Re: [Tutor] changing char list to int list isn't working

2013-05-15 Thread Bhanu Pratap Singh
Hi Jim,
When you replace num = int(num); it's not changing a list.
Due to It only pick a value from a list.
You have to pointing to list so,
You have to use list[position] using while loop like below

listOfNumChars = list(str(intNum))
i = 0
while i  len(listOfNumChars):
listOfNumChars[i] = int(listOfNumChars[i])
i += 1

print(listOfNumChars)

#-- [4, 5, 5] (if you entered 455)


Best,
Bhanu Pratap


-Original Message-
From: Tutor [mailto:tutor-bounces+bhanubais=gmail@python.org] On Behalf Of 
Jim Mooney
Sent: Saturday, May 04, 2013 9:43 AM
To: tutor@python.org
Subject: [Tutor] changing char list to int list isn't working

I'm turning an integer into a string so I can make a list of separate chars, 
then turn those chars back into individual ints, but the resulting list still 
looks like string chars when I print it. What am I doing wrong?


listOfNumChars = list(str(intNum))
for num in listOfNumChars:
num = int(num)

print(listOfNumChars)

# result of 455 entered is ['4', '5', '5']

--
Jim Mooney

“For anything that matters, the timing is never quite right, the resources are 
always a little short, and the people who affect the outcome are always 
ambivalent.”
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-04 Thread Alan Gauld

On 04/05/13 05:13, Jim Mooney wrote:

I'm turning an integer into a string so I can make a list of separate
chars, then turn those chars back into individual ints,


You don't actually need to convert to chars, you could
use divmod to do it directly on the numbers:

 digits = []
 root = 455
 while root  0:
... root, n = divmod(root,10)
... digits.insert(0,n)
...
 digits
[4, 5, 5]

But I suspect the str() method is slightly faster...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-04 Thread Mitya Sirenef

On 05/04/2013 12:13 AM, Jim Mooney wrote:

for num in listOfNumChars:

 num = int(num)


It seems like people learning Python run into this very often.

I think the reason is that in most simple cases, it's easier and more
intuitive to think that the name IS the object:

x = 1
y = 2
print x + y

Even though I know it's not a precise description, when I see this code,
I think of it as x is 1, y is 2, print x plus y. And you do get
expected result, which reinforces this intuition.

Of course, a more precise way to think is:

 name 'x' is assigned to object with value=1
 name 'y' is assigned to object with value=2
 sum values that currently have assigned names of 'x' and 'y'

Therefore, what you are really doing is:

for each object in listOfNumChars:
assign name 'num' to object (this is done automatically by the loop)
assign name 'num' to int(value that has currently assigned name 'num')


 -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Oaths are the fossils of piety.  George Santayana

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] changing char list to int list isn't working

2013-05-03 Thread Jim Mooney
I'm turning an integer into a string so I can make a list of separate
chars, then turn those chars back into individual ints, but the
resulting list still looks like string chars when I print it. What am
I doing wrong?


listOfNumChars = list(str(intNum))
for num in listOfNumChars:
num = int(num)

print(listOfNumChars)

# result of 455 entered is ['4', '5', '5']

--
Jim Mooney

“For anything that matters, the timing is never quite right, the
resources are always a little short, and the people who affect the
outcome are always ambivalent.”
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-03 Thread eryksun
On Sat, May 4, 2013 at 12:13 AM, Jim Mooney cybervigila...@gmail.com wrote:
 I'm turning an integer into a string so I can make a list of separate
 chars, then turn those chars back into individual ints, but the
 resulting list still looks like string chars when I print it. What am
 I doing wrong?

 listOfNumChars = list(str(intNum))
 for num in listOfNumChars:
 num = int(num)

 print(listOfNumChars)

 # result of 455 entered is ['4', '5', '5']

The body of your for loop only rebinds the loop variable. It's not
appending to a new list or modifying listOfNumChars. As to the latter
list, it's redundant since a string is iterable.

The following snippet creates the list [4, 5, 5]:

num = 455

numlist = []
for c in str(num):
numlist.append(int(c))

or using a list comprehension:

numlist = [int(c) for c in str(num)]

or using map:

numlist = list(map(int, str(num)))

iterators
http://docs.python.org/3/library/stdtypes.html#typeiter

built-in functions
http://docs.python.org/3/library/functions.html#iter
http://docs.python.org/3/library/functions.html#map

for statement
http://docs.python.org/3/reference/compound_stmts.html#the-for-statement

comprehensions
http://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] changing char list to int list isn't working

2013-05-03 Thread Steven D'Aprano

On 04/05/13 14:13, Jim Mooney wrote:

I'm turning an integer into a string so I can make a list of separate
chars, then turn those chars back into individual ints, but the
resulting list still looks like string chars when I print it. What am
I doing wrong?

listOfNumChars = list(str(intNum))


This creates a list of characters.



for num in listOfNumChars:
 num = int(num)


This walks over the list, setting the variable num to each character in turn, then inside 
the loop you set the variable num to the converted char-int. But num isn't linked to 
the list in any way -- num has no memory that the value it got came from a list. Reassigning num 
inside the loop doesn't touch the list in any way, so naturally the list doesn't change.



print(listOfNumChars)

# result of 455 entered is ['4', '5', '5']



To split a number into digits, the shortest way is to use a list comprehension:


digits = [int(c) for c in str(num)]


We can expand that list comp into a for-loop:


digits = []
for c in str(num):
digits.append(int(c))



Notice that there is no need to convert the string into a list. You can iterate 
over the characters of a string just fine.

If you prefer to create a list, then modify it in place, we can do this:


digits = list(str(num))
for position, char in enumerate(digits):
digits[position] = int(char)



Here we use enumerate() to iterate over pairs of (position, value) instead of 
just value:

py list(abcd)
['a', 'b', 'c', 'd']
py list(enumerate(abcd))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]



Here's another way, using map:


digits = map(int, str(num))  # Python 2.x only

digits = list(map(int, str(num)))  # Python 3.x or better



Why the difference between Python 2.x and 3.x? In 2.x, map is eager, it runs all the 
way through the string as soon as you call it, returning a list. In 3.x, map is lazy, 
and only generates values when and as needed. By wrapping the map generator in a call to list, that 
forces it to run all the way through the string.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor