[英]Create arrays of fixed size within a while loop in python
我試圖在while循環中創建固定大小的數組。 由於我不知道必須創建多少個數組,因此我使用循環在while循環中初始化它們。 我面臨的問題是數組聲明,我希望每個數組的名稱以while循環的索引結尾,因此以后對我的計算很有用。 我不希望找到一個簡單的出路,但是如果有人可以向我指出正確的方向,那就太好了
我嘗試使用arrayname + str(i)。 這將返回錯誤“無法分配給操作員”。
#parse through the Load vector sheet to load the values of the stress vector into the dataframe
Loadvector = x2.parse('Load_vector')
Lvec_rows = len(Loadvector.index)
Lvec_cols = len(Loadvector.columns)
i = 0
while i < Lvec_cols:
y_values + str(i) = np.zeros(Lvec_rows)
i = i +1
我希望創建名稱為arrayname1,arrayname2 ...的數組。
我認為標題有些誤導。
一種簡單的方法是使用字典:
dict_of_array = {}
i = 0
while i < Lvec_cols:
dict_of_array[y_values + str(i)] = np.zeros(Lvec_rows)
i = i +1
您可以通過dict_of_array[arrayname1]
訪問arrayname1
。
如果要創建一批數組,請嘗試:
i = 0
while i < Lvec_cols:
exec('{}{} = np.zeros(Lvec_rows)'.format(y_values, i))
i = i +1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.