簡體   English   中英

python gensim:AttributeError:'列表'對象沒有屬性

[英]python gensim: AttributeError: 'list' object has no attribute

我有一個小的python管道。 一類清除數據並對其進行定形。 它返回一個字符串列表列表(即List[List[str]] )。 然后,我將列表傳遞給另一個類,該類將數據傳遞給gensim字典

但是,以下代碼將引發此異常:

dictionary = corpora.Dictionary(self.bowlist)
AttributeError: 'list' object has no attribute 'bowlist'

碼:

from typing import List
import re
from gensim import corpora

class ListOfListsToGensimCorpora:
    def __init__(self, bow_list: List[List[str]]):
        self.bowlist = bow_list

    def perform(self):
        dictionary = corpora.Dictionary(self.bowlist)
        print(dictionary)

我是Python的新手,但我已經通過調試和其他方法進行了檢查,bowlist是List [List [str]]。

你這樣用嗎?

ListOfListsToGensimCorpora.perform(bow_list)

這樣做很好:

l = ListOfListsToGensimCorpora(bow_list)
l.perform()

或以此更改您的代碼?

from typing import List
import re
from gensim import corpora

class ListOfListsToGensimCorpora:
    def __init__(self, bow_list: List[List[str]]):
        self.bowlist = bow_list
        self.perform()  # run perform when instantiuation 

    def perform(self):
        dictionary = corpora.Dictionary(self.bowlist)
        print(dictionary)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM