【问题标题】:Python array manipulation, pi*[n+1]^2 - pi*[n]^2Python 数组操作,pi*[n+1]^2 - pi*[n]^2
【发布时间】:2016-08-17 23:44:22
【问题描述】:

我正在编写一个脚本来从多个圆柱体的外部圆柱体中减去内部圆柱体。

例如:x = pi*[n+1]**2 - pi*[n]**2

但是我不确定如何让 n 每次从例如 1 到 4 进行更改,我希望能够更改 n 并让代码在新值中运行而无需更改所有内容。

x = pi*[1]**2 - pi*[0]**2    
x = pi*[2]**2 - pi*[1]**2
x = pi*[3]**2 - pi*[2]**2
x = pi*[4]**2 - pi*[3]**2

我试图让一个 while 循环工作,但我无法弄清楚如何引用 n 而不具体说明我想引用数组中的哪个数字。

任何帮助将不胜感激。

rs = 0.2                                # Radius of first cylinder  
rc = 0.4                                # Radius of each cylinder (concrete) 
rg = 1                                  # Radius of each cylinder (soil)
BW = 3                                  # No. cylinders (concrete)
BG = 2                                  # No. cylinders (soil)
v1 = np.linspace(rs, rc, num=BW)        # Cylinders (concrete)
v2 = np.linspace(rc * 1.5, rg, num=BG)  # Cylinders (soil)

n = np.concatenate((v1, v2))            # Combined cylinders

for i in range(BW + BG):
    x = np.pi * (n[i + 1] ** 2) - np.pi * (n[i] ** 2)

【问题讨论】:

  • 为什么不能具体说明要引用的数组编号?提供更多上下文代码会很有用。

标签: python arrays loops


【解决方案1】:

试试这个:

for n in range(4): # 0 to 3
    x = pi*[n+1]**2 - pi*[n]**2 #[1] - [0], [2] - [1] and so on...
    # doSomething

如果[n] 是名称为num 的数组的索引,请将[n] 替换为 num[n] 像这样:

for n in range(4): # 0 to 3
    x = pi*(num[n+1]**2) - pi*(num[n]**2) #[1] - [0], [2] - [1] and so on...
    # doSomething

如果它只是 n,请将 [n] 替换为 n,如下所示:

for n in range(4): # 0 to 3
    x = pi*((n+1)**2) - pi*(n**2) #[1] - [0], [2] - [1] and so on...
    # doSomething

【讨论】:

  • pi*[n]**2 没有任何意义,因为[n] 是一个单元素列表。我认为提问者的意思是将其作为索引,所以请改用pi * some_sequence[n]**2
  • 感谢您的及时回复,但是它们是浮点数,有没有一种简单的方法可以使范围与浮点数一起使用?
  • @ScottLines 我不明白你说的enable range to work with floats?是什么意思
  • 我得到一个错误,'float' 对象不能被解释为一个整数
  • @ScottLines 我无法重现该问题。请输入您的完整代码好吗?
【解决方案2】:

由于您的数字位于一个 numpy 数组中,因此在整个数组(或数组的切片)中使用广播操作会更有效,而不是编写显式循环并对单个项目进行操作。这是使用 numpy 的主要原因!

试试这样的:

# compute your `n` array as before

areas = pi * n**2   # this will be a new array with the area of each cylinder
area_differences = areas[1:] - areas[:-1]   # differences in area between adjacent cylinders

【讨论】:

    【解决方案3】:

    这个怎么样:

    for i, value in enumerate(n[:-1]):
        print(np.pi * (n[i + 1] ** 2) - np.pi * (value ** 2))
    

    对我来说,它会打印:

    0.157079632679
    0.219911485751
    0.628318530718
    2.0106192983
    

    也许你想要这个:

    >>> values = [np.pi * (n[i + 1] ** 2) - np.pi * (value ** 2)
                              for i, value in enumerate(n[:-1])]
    >>> values
    [0.15707963267948971, 0.2199114857512855, 0.62831853071795885, 2.0106192982974673]
    

    让我们解释一下:

    • 我们必须获取列表中除最后一个以外的所有元素,因为n[i + 1] 对最后一项失败,所以我们使用n[0:-1](如果切片为 0,则允许省略切片的开头,如果为 0,则可以省略结尾等于或大于len(n))。
    • enumerate(a_list) 返回类似于表单中对列表的内容
      [(0, a_list[0]), (1, a_list[1]), ..., (n, a_list[n)]
    • for i, value in ... 将每一对解包到名为 ivalue 的变量中
    • [something for something in a_list] 返回一个新列表。您可以进行计算并过滤值。例如,如果您想要 10 以下的偶数平方的列表:
      >>> [x * x for x in range(10) if x % 2 == 1]
      [1, 9, 25, 49, 81]

    【讨论】:

    • 效果很好,我需要将值保存在数组中而不是打印出来。
    • 已更新。秘密是跳过最后一个值 (n[:-1]),因为它缺少下一项。最后一个 sn-p 中的列表推导的这种语法受到数学中的集合构建符号的启发,所以如果你喜欢数学,它一定很熟悉。
    • 谢谢,这正是我所追求的。
    【解决方案4】:

    我认为这应该提供您正在寻找的结果:

    rs = 0.2                                # Radius of first cylinder
    rc = 0.4                                # Radius of each cylinder (concrete) 
    rg = 1                                  # Radius of each cylinder (soil)
    BW = 3                                  # No. cylinders (concrete)
    BG = 2                                  # No. cylinders (soil)
    v1 = np.linspace(rs, rc, num=BW)        # Cylinders (concrete)
    v2 = np.linspace(rc * 1.5, rg, num=BG)  # Cylinders (soil)
    
    n = np.concatenate((v1, v2))
    
    results = []
    for i, v in enumerate(n):
        if i+1 < len(n):
            results.append(pi * n[i+1] ** 2 - pi * v ** 2)
        else:
            break
    

    【讨论】:

    • 这正是我所追求的,有没有一种简单的方法可以更改它,以便将值保存在数组中而不是覆盖它?