![](https://secure.gravatar.com/avatar/e70ec656475c822b364869ab31796d38.jpg?s=120&d=mm&r=g)
Hi everyone I'm currently writing some code where I read in a data file - Generally there are > 1 entry and I check certain parameters for example if data[0]['date'] == something: Which is fine until I get a single element and the above gives: '0-d arrays cannot be indexed' which makes sense , however, is there any easy way of just using the above code without having to check each time to see if it is only a single element - i.e. transparent (similar to idl) and just assumes that data[0] is the only element if there is only one in there. Cheers Ross
![](https://secure.gravatar.com/avatar/ad4ab4eded6bcd5d60bacc446419a4fd.jpg?s=120&d=mm&r=g)
Hi, You could take a look at numpy.atleast_1d. ~ Roger ~ Roger On Mon, May 18, 2009 at 8:52 AM, Ross Williamson <Ross.Williamson@usap.gov> wrote:
Hi everyone
I'm currently writing some code where I read in a data file - Generally there are > 1 entry and I check certain parameters for example
if data[0]['date'] == something:
Which is fine until I get a single element and the above gives:
'0-d arrays cannot be indexed'
which makes sense , however, is there any easy way of just using the above code without having to check each time to see if it is only a single element - i.e. transparent (similar to idl) and just assumes that data[0] is the only element if there is only one in there.
Cheers
Ross _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/e70ec656475c822b364869ab31796d38.jpg?s=120&d=mm&r=g)
Hi Roger Thanks At the moment I check the length with if data.size == 1: data = data.reshape(1,) Which works so I shouldn't complain too much :) Just thought there might be a more obvious way of doing it Ross Roger Herikstad wrote:
Hi, You could take a look at numpy.atleast_1d.
~ Roger
~ Roger
On Mon, May 18, 2009 at 8:52 AM, Ross Williamson <Ross.Williamson@usap.gov> wrote:
Hi everyone
I'm currently writing some code where I read in a data file - Generally there are > 1 entry and I check certain parameters for example
if data[0]['date'] == something:
Which is fine until I get a single element and the above gives:
'0-d arrays cannot be indexed'
which makes sense , however, is there any easy way of just using the above code without having to check each time to see if it is only a single element - i.e. transparent (similar to idl) and just assumes that data[0] is the only element if there is only one in there.
Cheers
Ross _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/ad4ab4eded6bcd5d60bacc446419a4fd.jpg?s=120&d=mm&r=g)
Hi Ross, I've come across the same problem myself, and I found numpy.atleast_1d to be handy. At the very least, it saves me one extra line of code : ) ~ Roger On Mon, May 18, 2009 at 9:07 AM, Ross Williamson <Ross.Williamson@usap.gov> wrote:
Hi Roger
Thanks
At the moment I check the length with
if data.size == 1: data = data.reshape(1,)
Which works so I shouldn't complain too much :) Just thought there might be a more obvious way of doing it
Ross
Roger Herikstad wrote:
Hi, You could take a look at numpy.atleast_1d.
~ Roger
~ Roger
On Mon, May 18, 2009 at 8:52 AM, Ross Williamson <Ross.Williamson@usap.gov> wrote:
Hi everyone
I'm currently writing some code where I read in a data file - Generally there are > 1 entry and I check certain parameters for example
if data[0]['date'] == something:
Which is fine until I get a single element and the above gives:
'0-d arrays cannot be indexed'
which makes sense , however, is there any easy way of just using the above code without having to check each time to see if it is only a single element - i.e. transparent (similar to idl) and just assumes that data[0] is the only element if there is only one in there.
Cheers
Ross _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
participants (2)
-
Roger Herikstad
-
Ross Williamson