【问题标题】:How can I create a list of R=10000 booleans called inside in Python?如何创建在 Python 中调用的 R=10000 布尔值列表?
【发布时间】:2018-09-21 21:02:51
【问题描述】:
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, origin)函数是什么样的?
  • 距离函数是沿 x 和 y 的每个维度的平方差之和的平方根。 distance = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2)

标签: python python-3.x list boolean


【解决方案1】:

我不知道您的 distance 函数是什么样子,但假设您传递给函数的 x 变量有 10000 个点,这是一种计算布尔数组的方法。在调用函数in_circle 时,您可以传递所有10000 个点的数组/列表,并将in_circle([(1,1), (1,2), (2,2)]) 中的[(1,1), (1,2), (2,2)]) 替换为包含点的数组/列表。如果它不起作用,请告诉我。

在此解决方案中,您将通过将所有 10000 个点一起传递来调用函数 in_circle 一次,然后 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)])

【讨论】:

  • 感谢您的评论,距离函数是沿 x 和 y 的每个维度的平方差之和的平方根。距离 = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2)
【解决方案2】:

据我了解,您有这样的元组列表

l = [(x1, y1),
     (x2, y2),
     ...
     (x10000, y10000)]

在这种情况下,您需要对其进行迭代
但首先,创建一个包含布尔值的列表

bools = []
for xy in l: # xy is tuple
    bools.append(in_circle(xy))

那是初级水平,但还有更高级的方法:

bools = [in_circle(xy) for xy in l]

【讨论】:

    【解决方案3】:

    您好,只需将您的 1000 个点或任意数量的坐标放在下面的列表 li 中,然后使用循环在它们上运行您的函数,您将在列表 X 中获得布尔值。喜欢:

     x=[] #declare an empty list where boolean value needs to be stored
    li=[(1,1),(0,0),(-1,1)]
    
    for i in range(0,len(li)):
        x1=in_circle(li[i])
        x.append(x1)
    

    【讨论】:

    • 如果有任何帮助,请接受并投票赞成答案。谢谢:)
    • 您在此处的列表中究竟将布尔值存储在何处?请阅读问题标题。也许您可以根据 OP 的需要修改您的代码
    • @Bazingaa 感谢您的建议。根据您的建议编辑了答案。谢谢。
    • 还不正确。在我指出错误之前,尝试在解决方案的第 4 行和第 5 行找到错误
    • 您可以坚持使用for i in li: 并使用x1=in_circle(i),因为您没有在任何地方使用索引。所以这里不需要使用range
    【解决方案4】:

    如果是编程初学者,你会发现看看这些概念很有用,它们会在你的程序员未来得心应手:

    maplambdagenerators

    1. 您可以使用 lambda 函数定义简单的函数。

    作为:

    in_circle = lambda (x, y): True if Math.sqrt( Math.pow(x,2) + Math.pow(y,2) ) < 1 else False
    # assuming circle has center 0, 0  and radius 1
    

    2。 然后将函数映射到点列表:

    map( in_circle, your_list)
    

    请注意,在 lambda 中,语法 (x, y) 是因为您将元组作为一个参数传递,并且您的列表形成如下:

    your_list = [(0,1),(0, 0.5), (0.3, 0.4) ...]
    

    3。 除了列表,您还可以使用生成器,如果您不需要再次使用列表,这种结构在迭代中非常方便。

    语法类似(注意括号!(VS [)

    your_point = [ (random.random(), random.random()) for i in range(n)  ] 
    # list comprehension
    
    your_point = (  (random.random(), random.random()) for i in range(n)  )
    # generator
    

    4。 因此,您可以生成一个包含 N 个布尔值的列表,例如:

    n = 10000
    your_point = ( (random.random(), random.random()) for i in range(n) )
    bool_list = map( in_circle, your_list)
    

    如果您想了解 lamdba 和常规函数之间的区别,另请参阅: what is the difference for python between lambda and regular function?

    为了您对生成器 VS 列表理解的兴趣: Generator Expressions vs. List Comprehension

    【讨论】:

    • 为什么这个答案被选为正确的,现在我收到了 Stackoverflow 的通知,我的声誉为 -15 分,因为 OP 改变了主意?除了这个答案是正确的之外,我还努力解释了 python 中的其他关键概念,因为 OP 说“我的编程水平 - 初学者”。呸 .. 哦,好吧,这不会是一场戏剧 - 感谢 SO 社区,我学到了很多东西,并希望回报。
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2021-03-12
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多