By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,587 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,587 IT Pros & Developers. It's quick & easy.

Converting List of String to Integer

P: n/a
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
Jul 21 '08 #1
Share this Question
Share on Google+
12 Replies

P: n/a
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
Jul 21 '08 #2

P: n/a
On Jul 21, 3:20*pm, Gary Herron <gher...@islandtraining.comwrote:
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 -
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
Jul 21 '08 #3

P: n/a
On Jul 22, 6:11 am, Samir <spytho...@gmail.comwrote:
[snip]
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 ->')
>
print n
Jul 21 '08 #4

P: n/a
Samir wrote:
On Jul 21, 3:20 pm, Gary Herron <gher...@islandtraining.comwrote:
>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 -

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
--
http://mail.python.org/mailman/listinfo/python-list
Jul 21 '08 #5

P: n/a
On Jul 21, 4:44*pm, Gary Herron <gher...@islandtraining.comwrote:
Samir wrote:
On Jul 21, 3:20 pm, Gary Herron <gher...@islandtraining.comwrote:
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 -
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 endof 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
--
http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -

- 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
Jul 21 '08 #6

P: n/a
Samir wrote:
On Jul 21, 3:20 pm, Gary Herron <gher...@islandtraining.comwrote:
>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 -

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

Jul 21 '08 #7

P: n/a
On Jul 21, 6:15*pm, Andrew Freeman <alif...@gmail.comwrote:
Samir wrote:
On Jul 21, 3:20 pm, Gary Herron <gher...@islandtraining.comwrote:
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 -
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
--
http://mail.python.org/mailman/listinfo/python-list

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- Hide quoted text -

- Show quoted text -
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
Jul 21 '08 #8

P: n/a
Samir wrote:
On Jul 21, 6:15 pm, Andrew Freeman <alif...@gmail.comwrote:
>Samir wrote:
>>On Jul 21, 3:20 pm, Gary Herron <gher...@islandtraining.comwrote:

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 -

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
--
http://mail.python.org/mailman/listinfo/python-list
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- Hide quoted text -

- Show quoted text -

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
--
http://mail.python.org/mailman/listinfo/python-list
If it helps look at this:

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

like this:

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

It is more verbose and easier to read and they both do exactly the same
thing!

iPython is great, to install it you might try easy_install:
http://peak.telecommunity.com/DevCenter/EasyInstall

Then in a command line type:
easy_install ipython

Then, once it is complete, to use iPython type:
ipython

--
Andrew
Jul 21 '08 #9

P: n/a
Samir wrote:
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.htm...00000000000000) 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:
>>print n
<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.
--
Andrew

Jul 21 '08 #10

P: n/a
>>a = [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '0']]
>>[map(int, i) for i in a]
[[1, 2], [3], [4, 5, 6], [7, 8, 9, 0]]

On Jul 21, 9:06*pm, Samir <spytho...@gmail.comwrote:
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
Jul 22 '08 #11

P: n/a
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
Jul 22 '08 #12

P: n/a
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
Jul 22 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.