頁:
[1]
Python新手,有些問題
各位大大好,小弟最近在學習Python的數據分析,
但基礎程式一直出問題,所以來這裡請教各位大大了
錯誤資訊都還看不懂++
這是程式碼:
%%capture time_results
import random
for n in :
print ("n = {0}".format(n))
alist = range(n)
%time random.shuffle(alist)
然後是錯誤資訊:
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
---------------------------------------------------------------------------TypeError Traceback (most recent call last)<timed eval> in <module>()c:\users\hatsu\appdata\local\programs\python\python36-32\lib\random.py in shuffle(self, x, random) 269 if random is None: 270 randbelow = self._randbelow--> 271 for i in reversed(range(1, len(x))): 272 # pick an element in x[:i+1] with which to exchange x 273 j = randbelow(i+1)TypeError: object of type 'int' has no len()
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 原來的程式碼是給 Python2 的吧
Python3 和 Python2 的 range() 不一樣
Python3 的 range() 基本上是 Python2 的 xrange()
這東西不是一個 list (Python2 的 range() 回傳的是 list),所以會出錯
可以把 alist = range(n) 改成 alist = list(range(n))
頁:
[1]