import random, math
random.seed(1)
def in_circle(x, origin = [0]*2):
"""
This function determines if a two-dimensional point
falls within the unit circle.
"""
if len(x) != 2:
return "x is not two-dimensional!"
elif distance(x, origin) < 1:
return True
else:
return False
print(in_circle((1,1)))
接下来,我想使用函数“in_circle”确定 x 中的每个点是否落在以 (0,0) 为中心的单位圆内。我该怎么做? 我的编程水平 - 初学者
最佳答案
我不知道你的distance
怎么样函数看起来像但假设你的 x
传递给函数的变量有 10000 个点,这是计算 boolean 数组的一种方法。调用函数in_circle
时,您可以传递所有 10000 个点的数组/列表并替换 [(1,1), (1,2), (2,2)])
在in_circle([(1,1), (1,2), (2,2)])
通过包含点的数组/列表。如果不起作用请告诉我。
在此解决方案中,您将调用函数 in_circle
一次将所有 10000 个点一起传递,然后 for
循环将计算函数内部的距离。在我看来,这比输入 for
更好。在外部循环并为每个点一一调用该函数 10000 次。
import random, math
random.seed(1)
def in_circle(x, origin = [0]*2):
bool_array = []
for point in x: # assuming x contains 10000 points
if len(x) != 2:
print ("x is not two-dimensional!")
continue
elif distance(x, origin) < 1:
bool_array.append(True)
else:
bool_array.append(False)
return bool_array
bool_array = in_circle([(1,1), (1,2), (2,2)])
关于python - 如何创建在 Python 内部调用的 R=10000 boolean 值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52451039/