'设定' object不能解释为整数

时间:2017-08-16 05:10:35

标签: python-3.x

我有以下python代码:

text = "this’s a sent tokenize test. this is sent two. is this sent three? sent 4 is cool! Now it’s your turn."
from nltk.tokenize import sent_tokenize
sent_tokenize_list = sent_tokenize(text)

import numpy as np

lenDoc=len(sent_tokenize_list)

features={'position','rate'}
score = np.empty((lenDoc, 2), dtype=object)
score=[[0 for x in range(sent_tokenize_list)] for y in range(features)]
for i,sentence in enumerate(sent_tokenize_list):
    score[i,features].append((lenDoc-i)/lenDoc)

但是会导致以下错误:

 TypeError                                 Traceback (most recent call last) <ipython-input-27-c53da2b2ab02> in <module>()
      13 
      14 
 ---> 15 score=[[0 for x in range(sent_tokenize_list)] for y in range(features)]
      16 for i,sentence in enumerate(sent_tokenize_list):
      17     score[i,features].append((lenDoc-i)/lenDoc)

 TypeError: 'set' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:0)

ending获取int值。 range()是一个集合,因此会抛出错误。你用features犯了同样的错误。 range(sent_tokenize_list)是列表值而不是int。 如果您希望x和y成为sent_tokenize_listfeatures的索引,那么您必须使用:sent_tokenize_list 但是,如果您希望x和y为score=[[0 for x in range(len(sent_tokenize_list))] for y in range(len(features))]features的值,则必须从该行中删除sent_tokenize_list