Stripers247
Stripers247

Reputation: 2335

TypeError: unorderable types: float() < function()

I have a code comprised of two functions one that reads data and the other that counts it. Both functions run properly when run separately, but I get the error when I try to have the counter call the file reader. I would appreciate it if some one could tell me where I am goofing up. Thanks in advance

Error

File "C:\Documents and Settings\Read_File.py", line 50, in counter
Sx = ((25. < Xa) & (Xa < 100.)).sum()    #count what is in x range
TypeError: unorderable types: float() < function()

Code

for line in f:        #Loop Strips empty lines as well as replaces tabs with space
if line !='':    
  line = line.strip()
  line = line.replace('\t',' ')
  columns = line.split()
  for line in range(N):       #Loop number of lines to be counted
   x = columns[8]             # assigns variable to columns
   y = columns[18]
   z = columns[19]
   #vx = columns[]
   #vy = columns[]
   #vz = columns[]
   X.append(x)
   Y.append(y)                #appends data in list
   Z.append(z)
  
 Xa = numpy.array(X, dtype=float)                   #Converts lists to NumPy arrays
 Ya = numpy.array(Y, dtype=float)
 Za = numpy.array(Z, dtype=float)



 return(Xa,Ya,Za)         #returns arrays/print statement to test 
   
 
def counter(Xa):
 Sx = ((25. < Xa) & (Xa < 100.)).sum()    #count what is in x range
 Sy = ((25. < Ya) & (Ya < 100.)).sum()    #count what is in y range
 Sz = ((25. < Za) & (Za < 100.)).sum()    #count what is in z range

 return(print(Sx,Sy,Sz))

read_file(F)                              #function calls 
counter(read_file)

EDit

With the help of Lev and James the first problem was fixed now I get this error

  Sx = ((2. < Xa) & (Xa < 10.)).sum()    #count what is in x range
  TypeError: unorderable types: float() < tuple()

Is this because of the commas in the arrays? And if so how can I get around this?

Upvotes: 1

Views: 6243

Answers (2)

Joel Cornett
Joel Cornett

Reputation: 24788

Here:

    Sx = sum(item for item in Xa if 25. < item < 100.)

... and so on and so forth.

Basically you're iterating through each item in Xa and omitting the ones that don't fall with the range. Then you're summing them.

P. S. You're getting the error because float() and tuple() types don't have a defined comparison function.

Upvotes: 1

James Aylett
James Aylett

Reputation: 3372

You are trying to call counter() on the function read_file(), not on the results of calling read_file(F). You don't include source for read_file(), but you almost certainly want to do:

counter(readfile(F))

instead of the last two lines. (By the way, the result(print(...)) in counter() probably doesn't need the return wrapping round the rest of it.)

Upvotes: 2

Related Questions