0

thats what I get:

TypeError: 'float' object is unsubscriptable

Thats what I did:

import numpy as N
import itertools

#I created two lists, containing large amounts of numbers, i.e. 3.465

lx = [3.625, 4.625, ...]
ly = [41.435, 42.435, ...] #The lists are not the same size!

xy = list(itertools.product(lx,ly)) #create a nice "table" of my lists

#that iterttools gives me something like
print xy 
[(3.625, 41.435), (3.625, 42.435), (... , ..), ... ]

print xy[0][0]
print xy[0][1] #that works just fine, I can access the varios values of the tuple in the list


#down here is where the error occurs
#I basically try to access certain points in "lon"/"lat" with values from xy through `b` and `v`with that iteration. lon/lat are read earlier in the script 

b = -1
v = 1

for l in xy:
    b += 1
    idx = N.where(lon==l[b][b])[0][0]
    idy = N.where(lat==l[b][v])[0][0]

lan/lot are read earlier in the script. I am working with a netCDF file and this is the latitude/longitude,read into lan/lot. Its an array, build with numpy.

Where is the mistake? I tried to convert b and v with int() to integers, but that did not help. The N.where is accessing through the value from xy a certain value on a grid with which I want to proceed. If you need more code or some plots, let me know please.

4
  • the problem is [0][0] after your where call Commented Mar 10, 2015 at 15:21
  • 1
    Where and what is lon and lat? Commented Mar 10, 2015 at 15:23
  • @user3012759 why is that a problem? Commented Mar 10, 2015 at 15:29
  • @Stophface sorry... got confused ignore my comment Commented Mar 10, 2015 at 15:32

1 Answer 1

1

Your problem is that when you loop over xy, each value of l is a single element of your xy list, one of the tuples. The value of l in the first iteration of the loop is (3.625, 41.435), the second is (3.625, 42.435), and so on.

When you do l[b], you get 3.625. When you do l[b][b], you try to get the first element of 3.625, but that is a float, so it has no indexes. That gives you an error.

To put it another way, in the first iteration of the loop, l is the same as xy[0], so l[0] is the same as xy[0][0]. In the second iteration, l is the same as xy[1], so l[0] is the same as xy[1][0]. In the third iteration, l is equivalent to xy[2], and so on. So in the first iteration, l[0][0] is the same as xy[0][0][0], but there is no such thing so you get an error.

To get the first and second values of the tuple, using the indexing approach you could just do:

x = l[0]
y = l[1]

Or, in your case:

for l in xy:
    idx = N.where(lon==l[0])[0][0]
    idy = N.where(lat==l[1])[0][0]

However, the simplest solution would be to use what is called "tuple unpacking":

for x, y in xy:
    idx = N.where(lon==x)[0][0]
    idy = N.where(lat==y)[0][0]

This is equivalent to:

for l in xy:
    x, y = l
    idx = N.where(lon==x)[0][0]
    idy = N.where(lat==y)[0][0]

which in turn is equivalent to:

for l in xy:
    x = l[0]
    y = l[1]
    idx = N.where(lon==x)[0][0]
    idy = N.where(lat==y)[0][0]
Sign up to request clarification or add additional context in comments.

4 Comments

I understand and I dont. How would I access then 3.625 and 41.435 differently? With that one it works: xy[0][0] and xy[0][1] With xy[1] I only access (3.625, 41.435)
that's because xy != l. xy is a list of tuples whereas l is a tuple. try to access l[0][0] in the loop and it will break
I have expanded the answer to explain how to actually make it work.
Ah, now I got it! Thanks a lot for your efforts!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.