python - 如何创建在 Python 内部调用的 R=10000 boolean 值列表?

标签 python python-3.x list boolean

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/

相关文章:

Python 正则表达式,模式匹配多个反斜杠字符

python - Python 中的 Pulseaudio 和 sudo

python - 我怎样才能让我的模型接受 2 个张量作为输入。我尝试过使用合并层,但我没有完全让它工作

python-3.x - 进入入口点后进入容器提示的命令

python - tk 消息框导入困惑

java - 如何打印从文本文件中按字母顺序读取的所有导演姓名或制片人姓名等

c++ - 计算机视觉 : Image Comparison and Counting?

python - 让 Ansible 信任我在 macOS 和 CentOS 上的自定义 CA

list - Scala - future 列表首先完成有条件

python - 获取 'TypeError: ' 列表“对象不可调用”错误