添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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()

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
                Ah I see! I failed to realise that "i" would in this case be a list instead of an item in the list! Thanks a lot, have a good day!
– Astudent
                Oct 24, 2018 at 19:04
                @Astudent - use print() to debug, or an IDE that allows breakpoints to inspect your program while executing. comment the "broken" part, print the stuff it operates on and look carefully next time to debug it
– Patrick Artner
                Oct 24, 2018 at 19:10
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.