0

I've defined a function as below to try to interpolate between two sets of data. When I run it, I get the message:

    for i, j in range(0, len(wavelength)):
TypeError: 'int' object is not iterable

I'm not sure what I'm doing wrong. Admittedly, I'm not very good at this.

def accountforfilter(wavelength, flux, filterwavelength, throughput):
    filteredwavelength=[]
    filteredflux=[]

    for i in range(0, len(wavelength)):
        if wavelength[i] in filterwavelength[j]:
            j=filterwavelength.index(wavelength[i])
            filteredwavelength.append(wavelength[i])
            filteredflux.append(flux[i]*throughput[j])

        elif wavelength[i]<filterwavelength[j]<wavelength[i+1]:
            m=((throughput[j+1]-throughput[j])/(filterwavelength[j+1]-filterwavelength[j])
            c=throughput[j]-(m*(wavelength[i]))
            filteredwavelength.append(wavelength[i])
            filteredflux.append(flux[i]*(m*wavelength[i]+c)

    return filteredwavelength, filteredflux
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
NXW
  • 61
  • 1
  • 5
  • http://stackoverflow.com/questions/18830145/typeerror-int-object-is-not-iterable-python-2-7 http://stackoverflow.com/questions/19523563/python-typeerror-int-object-is-not-iterable – Erik Kubica Dec 05 '14 at 12:36
  • Your traceback does *not* match the code you posted. Are you sure you are running the same code? – Martijn Pieters Dec 05 '14 at 12:37
  • I copied and pasted the code, so I assume I must be running the same as what is there. – NXW Dec 05 '14 at 12:43

3 Answers3

5

range() returns a list of integers. By using for i,j in range() you are telling Python to unpack each item in range() to two values. But since those values are integers, which are a single piece of data, and thus not iterable, you get the error message.

Your code also looks a bit strange. At first it seems like you want to loop over all combinations of wavelength/filterwavelength, which would be the same as

for i in range(len(wavelength)):
    for j in range(len(filterwavelength)):
         do_stuff()

but then you are modifying the j parameter inside the loop body, which I don't understand.

Regardless, there is probably a lot easier, and more clear, way to write the code you want. But from the current code it is hard to know what is expected (and should probably go in a separate question).

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
  • So if I instead replace that line with two if statements, one for i and one for j, would that work? – NXW Dec 05 '14 at 12:37
  • @NXW that depends, what are you trying to get `i` and `j` to be? You could use e.g. `for i, j in itertools.product(range(len(wavelenth)), repeat=2)` (see [here](https://docs.python.org/2/library/itertools.html#itertools.product)). – jonrsharpe Dec 05 '14 at 12:41
2

The problem is that the range only works with one variable like so:

for i in range(0, len(wavelength))

You try to use two variables at once so python tries to unpack an integer which is impossible. You should use the above. If you need two independent indices, use

for i in range(0, len(...))
  for j in range(0, len(...))

Btw ranges always start with zero so you can save yourself some typing and use range(len(...)) instead.

hfhc2
  • 4,182
  • 2
  • 27
  • 56
0

You can use zip if you want.

check this: Better way to iterate over two or multiple lists at once

if you have two sets of data.

for i,j in zip(set1,set2):
print i,j
Community
  • 1
  • 1
codename_subho
  • 456
  • 8
  • 22