2016-12-28 73 views
0

嗨,我只想從點列表中獲得點數。 步驟也在列表中,每一步都是不同的。蟒蛇2.7 - 切片從a到b比從b到c的列表,

例如:通過指數從0獲取點10比從11〜16

名單是示例。真正的名單要大得多。

這裏是我的代碼:

import clr 
clr.AddReference('ProtoGeometry') 
from Autodesk.DesignScript.Geometry import * 


number=[10,5,2,20,..,4) 
pointlist=[point1,point2,..,point900] 
result=[] 
a=0 
i=0 

for elem in number: 
    result.append(list[a:a+number[i]]) 
    i+=1 

print = result 

在我得到一個錯誤

TypeError: expected Array[Type], got slice 
in line "result.append(list[a:a+number[i]])" 

什麼即時做錯了的那一刻? 任何幫助將是偉大的!

下面是一個類似主題的鏈接:Explain Python's slice notation

+0

你不需要'i'和使用'[A:A + ELEM]' – furas

+0

'list'是創建列表的函數 - 你可能需要不同的名字,例如。 'pointlist' – furas

回答

1

你用錯了名字list,但你必須pointlist

而且你不需要i因爲你可以使用elem

number = [10, 5, 2, 20, 4] 

pointlist = ['point'+str(i) for i in range(1, 51)] 

result = [] 

a = 0 

for elem in number: 
    b = a + elem 
    result.append(pointlist[a:b]) 
    a = b 

print(result) 

結果

[ 
    ['point1', 'point2', 'point3', 'point4', 'point5', 'point6', 'point7', 'point8', 'point9', 'point10'], 
    ['point11', 'point12', 'point13', 'point14', 'point15'], 
    ['point16', 'point17'], 
    ['point18', 'point19', 'point20', 'point21', 'point22', 'point23', 'point24', 'point25', 'point26', 'point27', 'point28', 'point29', 'point30', 'point31', 'point32', 'point33', 'point34', 'point35', 'point36', 'point37'], 
    ['point38', 'point39', 'point40', 'point41'] 
] 
1

這是你在找什麼:

number=[10,5,2,20,12,15,6,9,12,14,4] 
pointlist=[2,4,7,10] 
result=[] 
index = 0 

for elem in pointlist: 
    result.append(number[index:elem]) 
    index = elem 
result.append(number[index:]) 
print(result) 

輸出:

[[10, 5], [2, 20], [12, 15, 6], [9, 12, 14], [4]] 
+0

Thx尋求幫助,但這不是我所需要的。看看你下面的furas答案。 – Yuli

+0

哦,好的。對於那個很抱歉。 –

相關問題