ValueError:位数必须大于零

时间:2015-04-13 05:59:55

标签: python-3.x random

我正在尝试编写一个关于pagerank的脚本.Below是我的脚本:

from scipy import *
from numpy import linalg as LA
import pickle
import random
import operator

def new_pagerank_step(current_page, N, d, links):

    if random.random() < 1 - d:
        next_page = random.randint(0, N)

    else:
        next_page = random.choice(links[current_page])

    return next_page




def pagerank_wikipedia_demo():
    with open("wikilinks.pickle", "rb") as f:
        titles, links = pickle.load(f)
    current_page = 2
    T = 100000
    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)

pagerank_wikipedia_demo()

但是,当我运行此脚本时,我收到此消息:

Traceback (most recent call last):
  File "/usr/lib/python3.2/random.py", line 249, in choice
    i = self._randbelow(len(seq))
  File "/usr/lib/python3.2/random.py", line 225, in _randbelow
    r = getrandbits(k)          # 0 <= r < 2**k
ValueError: number of bits must be greater than zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "markov1.py", line 141, in <module>
    pagerank_wikipedia_demo()
  File "markov1.py", line 135, in pagerank_wikipedia_demo
    current_page = new_pagerank_step(current_page, N, d, links)
  File "markov1.py", line 113, in new_pagerank_step
    next_page = random.choice(links[current_page])
  File "/usr/lib/python3.2/random.py", line 251, in choice
    raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence

我不明白ValueError的含义。对于IndexError,我知道这意味着链接[current_page]是空的,但是从我的代码中,我看不出为什么链接[current_page]可以是一个空列表。

2 个答案:

答案 0 :(得分:1)

请注意,random.randint(0, N)会返回一些x,以便0 <= x <= N

答案 1 :(得分:1)

抛出此错误是因为您以某种方式向random.choices提供空列表。尝试:

random.choice([])

你会看到同样的错误。

我不知道您的初始数据。但是我会看看你是否有空的链接列表,如果有的话,在代码中测试它并设计你需要做的事情。