TypeError:“map”和“int”的实例之间不支持“>”

2024-06-28 20:11:44 发布

您现在位置:Python中文网/ 问答频道 /正文

当我用3个整数参数运行下面的代码时

import sys
import numpy as np

fichier=open("vocLemma.tsv")
dico=dict()

borneMin=int(sys.argv[1])
borneMax=int(sys.argv[2])
nbClasseMax=int(sys.argv[3])
cpt=0

error=0
for ligne in fichier:
    try:
        tab=ligne.split("\t")
        mot=tab[0]
        freq=map(int, list(tab[1].strip("[").strip("]").replace(" ", "").split(",")))
        nbAnnee=int(tab[2])
        classes=tab[3]
        nbClasse=0
        for classe in ["A", "B", "C", "D", "E", "F", "G", "H"]:
            if classe in classes:
                nbClasse+=1
        docTotal=np.sum(np.array(freq))
        if not mot.isdigit() and nbAnnee > 1 and docTotal > borneMin and docTotal < borneMax and nbClasse <= nbClasseMax:
            dico[tab[0].strip()]=ligne.strip()
            cpt+=1
    except IndexError:
        error+=1
        pass
print(cpt)
fichier =open("voc_freqmin"+str(borneMin)+"_docmax"+str(borneMax)+"_classemax"+str(nbClasseMax), "w")
for mot in dico:
    fichier.write(dico[mot]+"\n")
fichier.close()

我收到一条错误信息,如:

File "filtre.py", line 25, in <module>
    if not mot.isdigit() and nbAnnee > 1 and docTotal > borneMin and docTotal < borneMax and nbClasse <= nbClasseMax:
TypeError: '>' not supported between instances of 'map' and 'int'

但我不知道为什么。有人能帮我吗?谢谢!你知道吗


解决了。我在@randomfigure中添加了这样一个注释,“这是人们在从python2移植到python3时遇到的一个常见错误”(也就是说,使用‘map’)


Tags: andinnpsystabdicointstrip
1条回答
网友
1楼 · 发布于 2024-06-28 20:11:44

map函数返回一个map对象,而不是一个列表。在此行中:

freq=map(int, list(tab[1].strip("[").strip("]").replace(" ", "").split(",")))

freq定义为map对象。稍后,当您通过numpy操作传递它时,它仍然只是单个map对象的数组。这条线:

docTotal=np.sum(np.array(freq))

不返回数字,而是返回另一个map对象。你知道吗

可以使用以下方法修复此问题:

freq=list(map(int, list(tab[1].strip("[").strip("]").replace(" ", "").split(","))))

相关问题 更多 >