回答
此行引發上述異常
arr = list(50)
list
期望一個iterable
對象(東西,有一個__iter__()
方法)。改爲傳遞int
。
要創建一個大小爲50的列表,你可以使用*
經營者名單上:
arr = [None] * 50
這將創建一個設置爲None
所有50個元素的列表。
解決你的問題更Python的方式是直接通過range
對象爲list
構造,如:
def odd_count(n):
return list(range(1,n+1, 2))
print(odd_count(50))
可以初始化括號中的列表。在for循環中,使用.append()
。最終的代碼應該是
def odd_count(n):
arr = []
for i in range(0,n,2):
arr.append(i)
return arr
認同,先生 – grovina
@PatrickHaugh:這個問題在標題中說得很奇怪,甚至在體內也是如此。 – ShadowRanger
您也可以使用Python的列表理解,如果你想成爲Python化:
def even_numbers(n):
return [i for i in range(0,n,2)]
No-op listcomps並非真正的Pythonic;如果它是沒有過濾器的身份轉換,那麼你只需要執行'list(range(0,n,2))',這個更快(除了最小輸入之外的所有輸入),更短和等價。 – ShadowRanger
- 1. operator <<:std :: cout << i <<(i << 1);
- 2. Assertion Failed <0 <= i && i < <int> vv.size <>> in unknown function,file src \ matrix.cpp,line 912
- 3. java.lang.ArithmeticException:</p> <pre><code>for (int i = Math.abs(key.hashCode()) % size; i < size; i++) </code></pre> <p>爲什麼會這樣:/零
- 4. 如何將此行「XX <i> \ r</i><i> \ n</i>」過濾爲XX?
- 5. self.class_eval << DEF ... DEF
- 6. 的Rails搞亂我的代碼以「<<<<<<< HEAD」
- 7. if(<object> == <int>)
- 8. $(e.target)是<i></i>而不是包含<div>...</div>
- 9. 如何(我<< 48)| ((i&0xffff0000L)<< 16) | ((i > >> 16)&0xffff0000L)| (i >>> 48)工作?
- 10. 爲什麼對於(int i = 0; i <10; ++ i)和for(int i = 0; i <10; i ++)返回相同?
- 11. 這個C++代碼是什麼意思是「sol <?= f((1 << n)-1,i,0)+ abs(P [i])* price;」
- 12. 角:</p> <pre><code><i style="cursor:pointer" ng-click="addName()" class="icon-plus"></i> <i style="cursor:pointer" ng-click="delName({{$index}})" class="icon-remove"></i> </code></pre> <p>而下面的功能在我的控制器的<code>$scope</code>:使用參數不工作
- 13. 計算a [i] < a[j] > a [k]的有效算法,其中i <j <k在數組中
- 14. << <<超載
- 15. print <<< HTML in php?
- 16. CUDA - 指定<<<x,y> >> for for循環
- 17. 超載<<我
- 18. JsonStore隨機拋出loadexception <i></i>
- 19. 如何使用<i></i>標記
- 20. 重載算子<<對於數組
- 21. 使用<<運算符
- 22. vector <pair <int,unordered_set <int> >>爲對
- 23. 分割錯誤過載ostream(<<)
- 24. 重載運算符<<數組
- 25. visual studios cout << not working
- 26. 如何在<a href="http://fontawesome.io/icons/" rel="nofollow noreferrer">http://fontawesome.io/icons/</a> 和<code><i class="fa fa-times"></i></code>內<asp:Button>
- 27. 使用>>,>,> |,||,| <,<,<<,<<
- 28. 正確轉換`的for(int i = 1; I <=大小;我++)`在R(情況下`大小<1`)
- 29. 變量從文本文件中讀取並傳遞給smarty顯示爲「Home <i> \ r</i><i> \ n</i>」?
- 30. matplotlib中的axes.flat是做什麼的?</p> <pre><code>for i, ax in enumerate(axes.flat): </code></pre> <p>這是什麼做的:
你的標題說,你想奇數,你的身體甚至說。這是什麼? – ShadowRanger
http://idownvotedbecau.se/beingunresponsive – klutt