简体   繁体   English

any()python中的多个for语句

[英]multiple for statements in any() python

So, I have four lists. 因此,我有四个列表。 Two hold x coordinates ( foodX and newPosXArray ) and the other two hold y coordinates ( foodX and newPosYArray ). 两个保持x坐标( foodXnewPosXArray ),另外两个保持y坐标( foodXnewPosYArray )。 Both the food and the newPos arrays are of different dimensions because I have multiple "food sources", and multiple objects searching for the food. 食物和newPos数组的维数不同,因为我有多个“食物来源”,并且有多个对象搜索食物。 I want to write an if statement that does something when the objects get to within a certain distance of the food. 我想编写一个if语句,当对象到达食物一定距离内时它会执行某些操作。 My attempt with any() 我对any()尝试

if any(np.sqrt((newPosXArray[u]-foodX[t])**2 + (newPosYArray[u]-foodY[t])**2) <= 0.2 for t in zip(food[0], food[1]) for u in zip(newPosXArray, newPosYArray)):
#dosomething

I am getting an error TypeError: list indices must be integers, not tuple 我收到一个错误TypeError:列表索引必须是整数,而不是元组

Edit: maybe I am misunderstanding zip(). 编辑:也许我误会zip()。 I was assuming that it condenses this 我以为它凝聚了这一点

 if any(np.sqrt((newPosXArray[u]-foodX[t])**2 + (newPosYArray[u]-foodY[t])**2) <= 0.2 for t in foodX for t in foodY for u in newPosXArray for u in newPosYArray):

Typical Values of what I am working with 我正在处理的典型值

foodX = [5,5,-5,-5]
foodY = [5,-5,5,-5]

In [65]: newPosXArray
Out[65]:
[-0.012880860643600167,
 -0.566815786730638,
 0.7905336304903405,
 0.09006991095474826,
 0.26518652615441063,
 0.3161232055076695,
 0.681255361368023,
 -0.6849985596071202,
 0.7140832628874829,
 0.4958515031060564]


In [66]: newPosYArray
Out[66]: 
[-0.41112817779983235,
 -0.08554837651693648,
 0.8743935617169996,
 -0.9384733737088207,
 0.02423386678116546,
 -0.3735855691077572,
 -0.5251118585489394,
 0.3950871276165102,
 0.9892320167752822,
 -0.7342372054958279]

of course, none of these values will return true in the if statement because none are within a 0.2 radius of any of the food coordinates 当然,这些值都不会在if语句中返回true,因为它们都不在任何食物坐标的0.2半径内

您尝试获取食物[] [z],但是z是元组(通过zip方法),也许您打算这样做

food[0][z[0]] 

The problem is that u and t are tuples because zip places x and y side by side in tuple format. 问题是u和t是元组,因为zip以元组格式并排放置x和y。 You need to say food[0][t[1]] and food[1][t[0]] where you intend to use the list values as coordinates. 您需要说出food[0][t[1]]food[1][t[0]] ,打算将列表值用作坐标。

From what it sees, you are trying to give it an x,(x,y) coordinate instead of an x,y coordinate. 从外观上看,您试图给它一个x,(x,y)坐标,而不是x,y坐标。

This holds where you are using u as a list index as well. 这也适用于将u用作列表索引的位置。

if any(np.sqrt((u[0] - t[0]) ** 2 + (u[1] - t[1]) ** 2) <= 0.2 for t in zip(foodX,foodY) for u in zip(newPosXArray,newPosYArray))

From values given it evaluates to False until the 0.2 value gets to 5, which sounds weird given how low your initial value was, but that's how the distance formula works. 从给定的值开始计算为False,直到0.2的值变为5,鉴于初始值太低,这听起来很奇怪,但这就是距离公式的工作原理。

Alternatively for readability you could change u to new and t to current . 另外,出于可读性考虑,您可以将u更改为new ,将t更改为current

Just to be clear nested loops and zip do not do the same thing: 只是为了清楚起见,嵌套循环和zip不会做相同的事情:

>>> [(i, j) for i in range(3) for j in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

Whereas: 鉴于:

>>> list(zip(range(3), range(3)))
[(0, 0), (1, 1), (2, 2)]

itertools.product does the same as next for loops: itertools.product与next for循环的作用相同:

>>> import itertools
>>> list(itertools.product(range(3), range(3)))
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

You don't index into the array when you iterate over an array, eg: 在数组上进行迭代时,您不会索引到数组中,例如:

>>> food = [1,2,3,4]
>>> [f+1 for f in food]     # Note you did [food[f] for f in food]
[2,3,4,5]

So fixing your second example looks like: 因此,修复第二个示例如下所示:

if any(np.sqrt((u_x-t_x)**2 + (u_y-t_y)**2) <= 0.2 
       for t_x in foodX for t_y in foodY for u_x in newPosXArray for u_y in newPosYArray):

But this iterates foodY for every foodX so assume you do want these to be zipped which would be written: 但这会迭代每个foodX的foodY,因此假设您确实希望压缩这些文件,将其写为:

if any(np.sqrt((u_x-t_x)**2 + (u_y-t_y)**2) <= 0.2 
       for t_x, t_y in zip(foodX, foodY) for u_x, u_y in zip(newPosXArray, newPosYArray)):

Or using itertools.product (which personally I don't find any easier to follow in this instance: 或使用itertools.product (就我个人而言,在这种情况下,我发现它更容易遵循:

import itertools
if any(np.sqrt((u_x-t_x)**2 + (u_y-t_y)**2) <= 0.2
       for (t_x, t_y), (u_x, u_y) in itertools.product(zip(foodX, foodY), zip(newPosXArray, newPosYArray))):

Of course, Pythonic way is to just use complex numbers instead of those 2D data separated along the wrong axis. 当然,Python方式是只使用复数,而不是沿错误轴分开的2D数据。 But if you really need to use that data format, I guess 但是,如果您真的需要使用该数据格式,我想

any(math.hypot(X-x,Y-y)<.2 for X,Y in zip(foodX,foodY) for x,y in zip(posX,posY))

will do what you want. 会做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM