【问题标题】:TypeError: object of type 'map' has no len() [duplicate]TypeError:“map”类型的对象没有 len() [重复]
【发布时间】:2019-01-06 02:16:07
【问题描述】:

我正在使用下面的函数来计算逆项频率。

all_tokens_set = set([item for sublist in tokenized_documents for item in sublist]) 

def inverse_document_frequencies(tokenized_documents):
idf_values = {}
global dfInv
dfInv = pd.DataFrame() 
tokenized_documents = [tokenize(d) for d in words]
all_tokens_set = set([item for sublist in tokenized_documents for item in sublist])
for tkn in all_tokens_set:
    contains_token = map(lambda doc: tkn in doc, tokenized_documents)
    idf_values[tkn] = 1 + math.log(len(tokenized_documents)/(sum(contains_token))) 
dfInv=pd.DataFrame(data ={'tkn':contains_token,'idf_values':idf_values[tkn]})
return dfInv
inverse_document_frequencies(total_all)

total_all 是一个列表。

我收到以下错误消息:

TypeError: 'map' 类型的对象没有 len()

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    为了找到长度,您必须将映射类型转换为列表(或元组),然后您可以调用 len 。例如:

     >>> len(list(map(lambda a: a[0], x)))
    3
    

    【讨论】:

    • 这不是类型转换,您正在使用迭代器。
    • 是的,我的意思是我们需要一个迭代器的实例来查找长度,所以我们只是将其转换为列表。我的意思是
    猜你喜欢
    • 2017-06-13
    • 2020-04-18
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2019-05-23
    • 2013-04-18
    • 2015-01-21
    • 2021-12-19
    相关资源
    最近更新 更多