2016-10-03 77 views
-1

我在開芝麻(python)寫一個inline_script。 誰能告訴我這裏有什麼問題嗎? (我認爲它的東西很簡單,但我找不到)開芝麻抵消

當我把數字放在List = [1,2,3,4,5,6,7]第一行的時候,但第二行不工作:(

BalanceList1 = range(1:7) + range(13:19) #does not work 
if self.get('subject_nr') == "BalanceList1": 

#here follows a list of commands 

BalanceList2 = list(range(7:13))+list(range(19:25)) #does not work either 
elif self.get('subject_nr') == "BalanceList2": 

#other commands 
+0

'BalanceList1 =範圍(1:6)+範圍(13:19)'會做你想要什麼。 –

+0

我仍然得到「無效的語法」 – SDahm

回答

1

在蟒蛇2.x的,你可以做到以下幾點:

BalanceList1 = range(1,6) + range(13,19) 

這將產生2所列出並在BalanceList1一起添加它們:

[1, 2, 3, 4, 5, 13, 14, 15, 16, 17, 18] 

在蟒蛇3.x中,range不返回list了,但一個迭代器(和xrange走了),你必須明確地轉換爲list

BalanceList1 = list(range(1,6))+list(range(13,19)) 

更加理想的方式,以避免產生過多的臨時名單將是:

BalanceList1 = list(range(1,6)) 
BalanceList1.extend(range(13,19)) # avoids creating the list for 13->18 

更優化比: