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

howto make Python list from numpy.array?

P: n/a
howto make Python list from numpy.array?
Thx, D.

May 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
dmitrey wrote:
howto make Python list from numpy.array?
Thx, D.
lst = map(None, arr) // for 1D arrays.

John Nagle
May 6 '07 #2

P: n/a

"dmitrey" <op*****@ukr.netwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
| howto make Python list from numpy.array?

I Googled 'numpy array Python list' and looked at the third hit
(PythonBasics) and at the bottom found your answer. About 30 seconds or
less.

tjr

May 6 '07 #3

P: n/a
Hello,

John Nagle a écrit :
dmitrey wrote:
>howto make Python list from numpy.array?
Thx, D.

lst = map(None, arr) // for 1D arrays.
Which can be expressed more simply as:
lst = list(arr)
--
Amaury
May 6 '07 #4

P: n/a
Thanks all.
I tried all the approaches but they don't work in my situation
I have a variable x that can be
x = 1
x = [1, 2, 3]
x = numpy.array([1,2,3])
so all troubles are with 1st case
>>x=1
list(x)
Traceback (most recent call last):
File "<string>", line 1, in <string>
TypeError: iteration over non-sequence
>>list(array(1))
Traceback (most recent call last):
File "<string>", line 1, in <string>
ValueError: Rank-0 array has no length.
>>array(1).tolist()
Traceback (most recent call last):
File "<string>", line 1, in <string>
ValueError: rank-0 arrays don't convert to lists.
>>>
Here I used Python 2.4.3, numpy 1.02

May 6 '07 #5

P: n/a
Amaury Forgeot d'Arc wrote:
Hello,

John Nagle a écrit :
>dmitrey wrote:
>>howto make Python list from numpy.array?
Thx, D.
lst = map(None, arr) // for 1D arrays.

Which can be expressed more simply as:
lst = list(arr)
For N-D arrays, that will give you a list of arrays. If you want a list of lists
(of lists of lists ... etc N times), use the .tolist() method of arrays.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 6 '07 #6

P: n/a
dmitrey wrote:
Thanks all.
I tried all the approaches but they don't work in my situation
I have a variable x that can be
x = 1
x = [1, 2, 3]
x = numpy.array([1,2,3])
so all troubles are with 1st case
>>>x=1
list(x)
Traceback (most recent call last):
File "<string>", line 1, in <string>
TypeError: iteration over non-sequence
>>>list(array(1))
Traceback (most recent call last):
File "<string>", line 1, in <string>
ValueError: Rank-0 array has no length.
>>>array(1).tolist()
Traceback (most recent call last):
File "<string>", line 1, in <string>
ValueError: rank-0 arrays don't convert to lists.

Here I used Python 2.4.3, numpy 1.02
All of these results are entirely correct. A scalar (or rank-0 array) is not a
sequence.

If you have a need to transform a scalar into a sequence (e.g. transform 1 to
[1]), then you can use numpy.atleast_1d(x).tolist().

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

May 6 '07 #7

P: n/a
On Sun, 06 May 2007 13:06:56 -0700, dmitrey wrote:
Thanks all.
I tried all the approaches but they don't work in my situation
I have a variable x that can be
x = 1
x = [1, 2, 3]
x = numpy.array([1,2,3])
so all troubles are with 1st case
And yet your question was about the third case:

"howto make Python list from numpy.array?"
You should have asked:

How do I make a Python list from an int?

And the answer would be:

x = 1
lst = [x]

Another answer would be:

x = 1
lst = ["existing", "list"]
lst.append(x)

--
Steven.

May 6 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.