假设我有一组元组,每个元组包含4个整数(A,B,C,D)
我有4个整数的输入元组(x,y,z,w)
我想列出集合中所有元素的列表
(abs(A - x) + abs(B - y) + abs(C - z) + abs(D - w)) / 4 <= i
其中i是用户定义的阈值。
我正在尝试另一个人从另一个问题获得的方法,那就是进行列表理解,我尝试了以下操作:
SET = my set of 4-tuples
input = the input tuple
for w in [element for element in SET
if ((sum(abs(x - y)) for x,y in zip(element,input)) / 4) <= i]:
Do something here
但是我不断收到错误消息,例如:
if ((sum(abs(x - y)) for x,y in zip(key,js)) / 4) == 0]:
TypeError: unsupported operand type(s) for /: 'generator' and 'int'
我不知道如何解决该问题,我查看了生成器的定义,它说生成器只是一个行为类似于迭代器的函数,我假设它是我的 sum(abs(x-y )),但这东西应该返回一个数字,我很困惑,请帮帮我,非常感谢!!!