在python中反击列表的单词

时间:2016-10-29 15:00:35

标签: python list loops filter counter

我有一个文本频率的计数器,变量名是frequenzaparolespamglob[]。我有原始输入parole[]的列表,我需要打印frequenzaparolespamglob[parole]。我知道如何只打印一个单词:

print (frequenzaparolespamglob['free']) # for example

但是,如果我只想打印计数器frequenzaparolespamglob中的假释实例。我能怎么做?我应该循环吗?

frequenzaparolehamglob=Counter()
i=0
while i < len(frequenzaparoleham):
    frequenzaparolehamglob= frequenzaparolehamglob + frequenzaparoleham[i]
    i=i+1

frequenzaparolespamglob=Counter()
i=0
while i < len(frequenzaparolespam):
    frequenzaparolespamglob= frequenzaparolespamglob + frequenzaparolespam[i]
    i=i+1

sommafreqspam= sum(frequenzaparolespamglob.values())

sommafreqham= sum(frequenzaparolehamglob.values())

frequenzaparolespamglob=sorted(frequenzaparolespamglob.items())
frequenzaparolehamglob=sorted(frequenzaparolehamglob.items())

inputemail = input('Inserisci una email:')
parole = nltk.word_tokenize(inputemail)
parole=[i for i in parole if i not in listastop]
parole = rimuovipunteggiatura.tokenize (str(parole))
parole=[i for i in parole if i.isalpha()]
parole=[ps.stem(parola) for parola in parole]
frequenzaparolespam=list(frequenzaparolespamglob)
risultato=Counter(frequenzaparolespamglob)
print(risultato[parole])

1 个答案:

答案 0 :(得分:0)

解决方案是:

filt_spam = {k:v for (k,v) in frequenzaparolespamglob.items() if k in parole}
filt_ham = {k:v for (k,v) in frequenzaparolehamglob.items() if k in parole}

现在我有另一个问题..在python 2.7中,人们只能打印频率的数量.. filter_ham.values()..现在我不能..任何人都可以说我怎么能只打印数字作为一个列出?