【问题标题】:TypeError: 'zip' object is not callable in PythonTypeError:“zip”对象在 Python 中不可调用
【发布时间】:2021-10-14 19:21:30
【问题描述】:

我正在尝试使用 Python 3.8 进行压缩,但不知道如何访问内容。我现在读到 zip() 是一个生成器,如果我想存储内容,我必须调用 dict(zip()) 。但是,我在执行此操作时遇到了错误。

例如:

from sklearn.feature_extraction.text import CountVectorizer
# initialise
count_vec = CountVectorizer()
word_list = []
for ques_split in [ques.split() for ques in group_wise_ques_list["dbo:genre"]]:
    # convert words to lower case
    word_list.extend(list(map(str.lower,ques_split)))
print(len(set(word_list)))

group_wise_ques_list = df.groupby(["Processedrelations"])["question"].apply(list)

# transform will normalise the data (if required) and fit will perform the matrix creation
genr_tdm = count_vec.fit_transform(group_wise_ques_list["dbo:genre"])
# convert the raw matrix into a dataframe
genr_tdm_df = pd.DataFrame(genr_tdm.toarray(), columns=count_vec.get_feature_names())


# for NLP, words itself are the features, hence get_feature_names will simply return the set of words
word_list_s = count_vec.get_feature_names(); 
# count the frequency of each word
count_list_s = genr_tdm.toarray().sum(axis=0) 
# create a dictionary with each word as key and its frequency as the value
freq_s = dict(zip(word_list_s,count_list_s))
# sort the dictionary in reverse order
freq_s = sorted(freq_s.items(), key = lambda x : x[1], reverse=True)
# display
freq_s

我收到了这个错误


TypeError: 'ZipFile' object is not callable

【问题讨论】:

  • 请包含您的代码的更多详细信息。是否有一个名为 zip 的属性,您已为其分配了一个 ZipFile 对象??
  • 我从 zipfile 中添加了 import ZipFile 但它不起作用
  • 哪一行出错了?
  • @xsrg45 您是否在代码中使用 zip 文件?请在您的问题中包含您的那部分代码。
  • 其打印的

标签: python nlp nltk


【解决方案1】:

我运行这段代码并没有收到错误:

count_list_s = [2,4,6,8,10,12] 
word_list_s = ['adf','bdfd','cdfd','ddfd','edfd','fdfdf']

freq_s = (dict(zip(word_list_s, count_list_s)))


freq_s = sorted(freq_s.items(), key = lambda x : x[1], reverse=True)
freq_s

输出:

[('fdfdf', 12),
 ('edfd', 10),
 ('ddfd', 8),
 ('cdfd', 6),
 ('bdfd', 4),
 ('adf', 2)]

【讨论】:

  • 如果你有这个from zipfile import ZipFile as zip,请确保删除这个命令并使用重启内核运行代码
  • 我得到了 ValueError: ZipFile 需要模式 'r'、'w'、'x' 或 'a'
  • @xsrg45,你在哪里运行你的代码?在不导入此压缩文件之前确定吗?
  • 在我导入压缩文件之前
  • @xsrg45,在tab kernel,点击restart and clear output,只运行我的代码,看看有没有报错?
猜你喜欢
  • 2018-05-14
  • 2018-08-02
  • 2015-09-14
  • 2020-03-24
  • 1970-01-01
  • 2016-08-17
  • 2017-12-20
相关资源
最近更新 更多