![](/img/trans.png)
[英]'>=' not supported between instances of 'list' and 'int'
[英]List comprehension and "not supported between instances of 'list' and 'float"
我正在嘗試使用列表理解方法來查找列表中大於我的變量之一的項目。
但是,我收到此錯誤消息:
TypeError: '>' not supported between instances of 'list' and 'float'
我不知道如何繞過它。 有小費嗎? 這是我的程序:
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()
alfa
是一個列表列表。
這個:
larger = [i for i in alfa if i > midy]
將內部列表列表中的一個列表i
與浮動midy
不支持。 這就是您的錯誤消息“not supported between instances of 'list' and 'float”
的確切含義。
我會將您的坐標從包含所有 x 和所有 y 的兩個內部列表加入到一個點列表(x,y)中,並過濾掉那些高於您的 midy 值的列表:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.