2016-08-17 44 views
1

我正在編寫一個腳本從多個氣瓶的外部氣瓶中減去內部氣瓶。Python數組操作,pi * [n + 1]^2 -pi * [n]^2

例如: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) 
+0

你爲什麼不能具體說明引用該序列號?提供更多的上下文代碼會很有用。 –

回答

2

試試這個:

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指數,與 num[n]取代[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 
+1

'pi * [n] ** 2'沒有任何意義,因爲'[n]'是一個元素列表。我認爲提問者的意思是做一個索引,所以把'pi * some_sequence [n] ** 2'代替。 – Blckknght

+0

感謝您的及時響應,但他們是浮動,有沒有一種簡單的方法來使範圍與浮動工作? – Scott

+0

@ScottLines我不明白你的意思是'啓用範圍與浮動工作?' –

-1

我想這應該提供你正在尋找的結果:

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 
+0

這正好提供了我後,有沒有一種簡單的方法改變它,使它保存在數組中的值而不是覆蓋它? – Scott

1

如何:

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或結束,如果它等於或大於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]
+0

工作得很好,我需要將值保存在一個數組中,而不是打印它們。 – Scott

+0

已更新。祕密是跳過最後一個值(n [: - 1]),因爲它缺少下一個項目。最後一個片段中的列表理解的語法是受數學中的set-builder符號的啓發,所以如果你使用數學,它一定很熟悉。 –

+0

謝謝,這正是我所追求的。 – Scott

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 
+0

可能是正確答案。 –

+0

也適用,謝謝你的幫助。 – Scott