我使用python3。 首先,我用random.choice在终端,和它的作品。
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> x = [1, 2, 3]
>>> random.choice(x)
3
但是,当我在我的脚本运行它,我得到的消息:
AttributeError: 'module' object has no attribute 'choice'
下面是代码
import random
from scipy import *
from numpy import linalg as LA
import pickle
import operator
def new_pagerank_step(current_page, N, d, links):
print(links[current_page])
if random.rand() > 1 - d:
next_page = random.choice(links[current_page])
else:
next_page = random.randint(0, N)
return next_page
def pagerank_wikipedia_demo():
with open("wikilinks.pickle", "rb") as f:
titles, links = pickle.load(f)
current_page = 0
T = 1000
N = len(titles)
d = 0.4
Result = {}
result = []
for i in range(T):
result.append(current_page)
current_page = new_pagerank_step(current_page, N, d, links)
for i in range(N):
result.count(i)
Result[i] = result.count(i) / T
Sorted_Result = sorted(Result.items(), key=operator.itemgetter(1))
pagerank_wikipedia_demo()
这里, links[i]
i
是整数)是一个列表。 当我运行此脚本时,出现上述消息。
我还检查了脚本的名称不是random
。 并且只有一个名为文件random.py
在/usr/lib/python3.2/random.py
为什么出现这种情况?