Converting List of String to Integer

S

Samir

Hi Everyone,

I am relatively new to Python so please forgive me for what seems like
a basic question.

Assume that I have a list, a, composed of nested lists with string
representations of integers, such that

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

I would like to convert this to a similar list, b, where the values
are represented by integers, such as

b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

I have unsuccessfully tried the following code:

n = []
for k in a:
n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir
 
G

Gary Herron

Samir said:
Hi Everyone,

I am relatively new to Python so please forgive me for what seems like
a basic question.

Assume that I have a list, a, composed of nested lists with string
representations of integers, such that

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

I would like to convert this to a similar list, b, where the values
are represented by integers, such as

b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

I have unsuccessfully tried the following code:

n = []
for k in a:
n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir

You didn't tell us how it failed for you, so I can't guess what's wrong.

However, your code works for me:
>>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>>> n = []
>>> for k in a:
.... n.append([int(v) for v in k])
....[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

(Although you seem to have confused variables b and n.)

Gary Herron
 
S

Samir

Samir said:
Hi Everyone,
I am relatively new to Python so please forgive me for what seems like
a basic question.
Assume that I have a list, a, composed of nested lists with string
representations of integers, such that
a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
I would like to convert this to a similar list, b, where the values
are represented by integers, such as
b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
I have unsuccessfully tried the following code:
n = []
for k in a:
    n.append([int(v) for v in k])
print n
Does anyone know what I am doing wrong?
Thanks in advance.

You didn't tell us how it failed for you, so I can't guess what's wrong.

However, your code works for me:

 >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
 >>> n = []
 >>> for k in a:
...    n.append([int(v) for v in k])
...
 >>> print n
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

(Although you seem to have confused variables b and n.)

Gary Herron- Hide quoted text -

- Show quoted text -

Hi Gary,

Thanks for your quick response (and sorry about mixing up b and n).
For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results. Below is the code in its entirety. Is
there a problem with my indendentation?

a = n = []
t = """
1 2
3
4 5 6
7 8 9 0
"""

d = t.split("\n")

for x in range(1,len(d)-1):
a.append(d[x].split(" "))
print a

for k in a:
n.append([int(v) for v in k])

print n

Thanks again.

Samir
 
J

John Machin

For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results. Below is the code in its entirety. Is
there a problem with my indendentation?

a = n = []
t = """
1 2
3
4 5 6
7 8 9 0
"""

d = t.split("\n")

for x in range(1,len(d)-1):
a.append(d[x].split(" "))
print a

for k in a:
n.append([int(v) for v in k])

To see what is happening, insert some print statements, plus something
to slow it down e.g.

for k in a:
print id(a), a
print id(n), n
n.append([int(v) for v in k])
raw_input('Hit Enter to continue ->')
 
G

Gary Herron

Samir said:
Samir said:
Hi Everyone,

I am relatively new to Python so please forgive me for what seems like
a basic question.

Assume that I have a list, a, composed of nested lists with string
representations of integers, such that

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

I would like to convert this to a similar list, b, where the values
are represented by integers, such as

b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

I have unsuccessfully tried the following code:

n = []
for k in a:
n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir
You didn't tell us how it failed for you, so I can't guess what's wrong.

However, your code works for me:
a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
n = []
for k in a:
... n.append([int(v) for v in k])
...
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

(Although you seem to have confused variables b and n.)

Gary Herron- Hide quoted text -

- Show quoted text -

Hi Gary,

Thanks for your quick response (and sorry about mixing up b and n).
For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results. Below is the code in its entirety. Is
there a problem with my indendentation?
Aha. There's the problem, right there in the first line.
a = n = []

This sets a and n to the *same* empty list. This line creates one
empty list and binds both n and a to that list. Note carefully, there
is only one empty list here, but it can be accessed under two names

Later in your code,

for k in a:

runs through that list, and

n.append(...)

append to the end of the same list. Thus the loop never get to the end of the (continually growing) list.

Solve it by creating two different empty lists:

a = []
n = []


Gary Herron




t = """
1 2
3
4 5 6
7 8 9 0
"""

d = t.split("\n")

for x in range(1,len(d)-1):
a.append(d[x].split(" "))
print a

for k in a:
n.append([int(v) for v in k])

print n

Thanks again.

Samir
 
S

Samir

Samir said:
Samir wrote:
Hi Everyone,
I am relatively new to Python so please forgive me for what seems like
a basic question.
Assume that I have a list, a, composed of nested lists with string
representations of integers, such that
a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
I would like to convert this to a similar list, b, where the values
are represented by integers, such as
b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
I have unsuccessfully tried the following code:
n = []
for k in a:
    n.append([int(v) for v in k])
print n
Does anyone know what I am doing wrong?
Thanks in advance.
Samir
--
http://mail.python.org/mailman/listinfo/python-list
You didn't tell us how it failed for you, so I can't guess what's wrong.
However, your code works for me:
 >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
 >>> n = []
 >>> for k in a:
...    n.append([int(v) for v in k])
...
 >>> print n
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
(Although you seem to have confused variables b and n.)
Gary Herron- Hide quoted text -
- Show quoted text -
Thanks for your quick response (and sorry about mixing up b and n).
For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results.  Below is the code in its entirety.  Is
there a problem with my indendentation?

Aha.  There's the problem, right there in the first line.
a = n = []

This sets a and n to the *same* empty list.    This line creates one
empty list and binds both n and a to that list.  Note carefully,  there
is only one empty list here, but it can be accessed under two names

Later in your code,

  for k in a:

runs through that list, and

  n.append(...)

append to the end of the same list.  Thus the loop never get to the end of the (continually growing) list.

Solve it by creating two different empty lists:

  a = []
  n = []

Gary Herron


t = """
1 2
3
4 5 6
7 8 9 0
"""
d = t.split("\n")
for x in range(1,len(d)-1):
    a.append(d[x].split(" "))
print a
for k in a:
    n.append([int(v) for v in k])
Thanks again.

- Show quoted text -- Hide quoted text -

- Show quoted text -

Gary,

That did the trick! I didn't realize that the way I initialized my
lists would lead to the behavior that I observed. After doing
something similar to what John had suggested I did indeed discover
that I created an endless loop. I'm glad I learned something today.

Thanks for your help.

Samir
 
A

Andrew Freeman

Samir said:
Samir said:
Hi Everyone,

I am relatively new to Python so please forgive me for what seems like
a basic question.

Assume that I have a list, a, composed of nested lists with string
representations of integers, such that

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

I would like to convert this to a similar list, b, where the values
are represented by integers, such as

b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

I have unsuccessfully tried the following code:

n = []
for k in a:
n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir
You didn't tell us how it failed for you, so I can't guess what's wrong.

However, your code works for me:
a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
n = []
for k in a:
... n.append([int(v) for v in k])
...
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

(Although you seem to have confused variables b and n.)

Gary Herron- Hide quoted text -

- Show quoted text -

Hi Gary,

Thanks for your quick response (and sorry about mixing up b and n).
For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results. Below is the code in its entirety. Is
there a problem with my indendentation?

a = n = []
t = """
1 2
3
4 5 6
7 8 9 0
"""

d = t.split("\n")

for x in range(1,len(d)-1):
a.append(d[x].split(" "))
print a

for k in a:
n.append([int(v) for v in k])

print n

Thanks again.

Samir
I think this will work better, a sub-list comprehension of sorts:
n = [[int(i) for i in k] for k in a]

here is an ipython interactive session using it:
In [1]: a = n = []

In [2]: t = """
...: 1 2
...: 3
...: 4 5 6
...: 7 8 9 0
...: """

In [3]:

In [4]: d = t.split("\n")

In [5]: for x in range(1,len(d)-1):
...: a.append(d[x].split(" "))
...:
...:

In [6]: a
Out[6]: [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

In [7]: n = [[int(i) for i in k] for k in a]

In [8]: n
Out[8]: [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
 
S

Samir

Samir said:
Samir wrote:
Hi Everyone,
I am relatively new to Python so please forgive me for what seems like
a basic question.
Assume that I have a list, a, composed of nested lists with string
representations of integers, such that
a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
I would like to convert this to a similar list, b, where the values
are represented by integers, such as
b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
I have unsuccessfully tried the following code:
n = []
for k in a:
    n.append([int(v) for v in k])
print n
Does anyone know what I am doing wrong?
Thanks in advance.
Samir
--
http://mail.python.org/mailman/listinfo/python-list
You didn't tell us how it failed for you, so I can't guess what's wrong.
However, your code works for me:
 >>> a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
 >>> n = []
 >>> for k in a:
...    n.append([int(v) for v in k])
...
 >>> print n
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]
(Although you seem to have confused variables b and n.)
Gary Herron- Hide quoted text -
- Show quoted text -
Thanks for your quick response (and sorry about mixing up b and n).
For some reason, the logic I posted seems to work ok while I'm using
the Python shell, but when used in my code, the program just hangs.
It never outputs the results.  Below is the code in its entirety.  Is
there a problem with my indendentation?
a = n = []
t = """
1 2
3
4 5 6
7 8 9 0
"""
d = t.split("\n")
for x in range(1,len(d)-1):
    a.append(d[x].split(" "))
print a
for k in a:
    n.append([int(v) for v in k])
Thanks again.

I think this will work better, a sub-list comprehension of sorts:
n = [[int(i) for i in k] for k in a]

here is an ipython interactive session using it:
In [1]: a = n = []

In [2]: t = """
   ...: 1 2
   ...: 3
   ...: 4 5 6
   ...: 7 8 9 0
   ...: """

In [3]:

In [4]: d = t.split("\n")

In [5]: for x in range(1,len(d)-1):
   ...:     a.append(d[x].split(" "))
   ...:    
   ...:    

In [6]: a
Out[6]: [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

In [7]: n = [[int(i) for i in k] for k in a]

In [8]: n
Out[8]: [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

Andrew,

Thanks for the tip, though the syntax makes my head spin a bit in
trying to comprehend it. For my small list, I didn't notice a
discernible increase in speed, but I may have to try it with a larger
list size.

Incidentally, I had never heard of iPython but from their web site, it
looks like an interesting tool. I'll have to check it out.

Thanks.

Samir
 
A

Andrew Freeman

Samir said:
For my small list, I didn't notice a
discernible increase in speed, but I may have to try it with a larger
list size.

About speed, and memory consumption:
List comprehensions
(http://docs.python.org/tut/node7.html#SECTION007140000000000000000) are
just shortcuts for for-loops. I do not believe there is any speed
benefit. However, there are generators, they basically load one part of
an iterator (a list in this case) at a time, this can greatly reduce
memory usage.
Have a look at PEP 289:
http://www.python.org/dev/peps/pep-0289/

Here is the list comprehension as a generator (actually 2):
n = ((int(i) for i in k) for k in a)


Note, you can't just print a generator, it only computes something when
needed:<generator object at 0xb7820e2c>

You can, however iterate over it:

In [2]: for k in n:
....: for i in k:
....: print i,
....:
....:
1 2 3 4 5 6 7 8 9 0
In [3]: n = ((int(i) for i in k) for k in a)
In [49]: list(n)
Out[49]:
[<generator object at 0xb77d03ec>,
<generator object at 0xb77d03cc>,
<generator object at 0xb77d046c>,
<generator object at 0xb77d04ac>]

Each sub-list is a generator too!
In [50]: n = ((int(i) for i in k) for k in a)
In [51]: for i in list(n): # list() converts the variable n to a list
....: list(i)
....:
....:
Out[51]: [1, 2]
Out[51]: [3]
Out[51]: [4, 5, 6]
Out[51]: [7, 8, 9, 0]


This is only going to make a difference if you were dealing with a
*very* large data set. I thought I would show you even if you never user
them, for learning purposes. Note: a generator is one way, redefine it
every time you use it.
 
D

dusans

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

Hi Everyone,

I am relatively new to Python so please forgive me for what seems like
a basic question.

Assume that I have a list, a, composed of nested lists with string
representations of integers, such that

a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]

I would like to convert this to a similar list, b, where the values
are represented by integers, such as

b = [[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

I have unsuccessfully tried the following code:

n = []
for k in a:
    n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir
 
P

ptn

n = []
for k in a:
    n.append([int(v) for v in k])
print n

Does anyone know what I am doing wrong?

Thanks in advance.

Samir

Use extend instead of append:

* Append -> add the one item to the end of the list
* Extend -> add the list of items to the end of the list
 
S

Samir

Wow! Thanks for all of the great additional feedback and responses
since I last checked in. The help this group provides is amazing.
I'm glad I found it.

@Andrew -- Thanks for the clarification on the nested for loop and how
to intrepret it. Also, thanks for the information on generators. I
have never come across this before so I will look into it a bit more.
The same thing goes for iPython. I have been looking for an IDE that
comes with better debugging capabilities that the default Python
shell.

@dusans -- Just when I thought I couldn't simplify my code anymore,
you provide come up with another way. Thank you.

@ptn -- Thanks for the explanation between "append" and "extend". I
guess I've gotten lazy and have always used "append", apparently even
in situations where it may not be appropriate.

Thanks again, everyone!

Samir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top