Astudent
Astudent

Reputation: 196

List comprehension and "not supported between instances of 'list' and 'float"

I'm trying the list comprehension method to find items in my list that are larger than one of my variables.

However, I get this error message:

TypeError: '>' not supported between instances of 'list' and 'float'

I don't know how to get around it. Any tips? Here is my Program:

def read_points():
    global alfa
    alfa = []
    alfa.append([])
    alfa.append([])
    a = 0
    b = 0
    a = float(a)
    b = float(b)
    print("Input the points, one per line as x,y.\nStop by entering an empty line.")
    while a == 0:
        start = input()
        if start == '':
            a = a + 1
            if b == 0:
                print("You did not input any points.")
        else:
            alfa[0].append(int(start.split(",")[0]))
            alfa[1].append(int(start.split(",")[1]))
            b = b + 1
    else:
        print(alfa)

def calculate_midpoint():
    midx = sum(alfa[0]) / len(alfa[0])
    global midy
    midy = sum(alfa[1]) / len(alfa[1])
    print("The midpoint is (",midx,",",midy,").")

def above_point():
    larger = [i for i in alfa if i > midy]   ###   PROBLEM IS HERE :)   ###
    number_above = len(larger)
    print("The number of points above the midpoint is", number_above)

def main():
    read_points()
    calculate_midpoint()
    above_point()

main()

Upvotes: 1

Views: 24020

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51683

alfa is a list of lists.

this:

larger = [i for i in alfa if i > midy] 

compares one of the inner lists list i against a float midy

which is not supported. Thats the exact meaning of your error message “not supported between instances of 'list' and 'float”.

I would join your coords from two inner lists holding all x and all y to a list of points(x,y) and filter those that lie above your midy value:

points = [ (x,y) for x,y in zip(*alfa) ]
larger = list(filter( lambda p: p[1] > midy, points)) # get all points that have y  midy

Upvotes: 3

Related Questions