首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何计算Python列表中出现的事件?

如何计算Python列表中出现的事件?
EN

Stack Overflow用户
提问于 2019-12-04 00:12:01
回答 4查看 685关注 0票数 2

我对python很陌生,我想计算每个单词在所有文件中出现的次数。显示每个单词、发生的次数和发生的时间百分比。对列表进行排序,使最频繁的单词首先出现,而最不频繁的单词出现在最后。我在做小样本,只知道一个文件,但我不能正常工作,

代码语言:javascript
运行
复制
 from collections import defaultdict

words = "apple banana apple strawberry banana lemon"

d = defaultdict(int)
for word in words.split():
    d[word] += 1
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-12-04 02:42:53

正如上面所建议的,来自collections模块的collections类无疑是计算应用程序的方法。

此解决方案还解决了使用fileinput.input()方法在多个文件中计数单词的请求,以迭代的所有内容--所有--命令行上指定的文件名(或者如果命令行上没有指定的文件名,则从STDIN (通常是键盘)读取)。

最后,它使用一种更复杂的方法将行拆分为“word”,并以正则表达式作为分隔符。正如代码中所指出的,它将更优雅地处理收缩(但是,它将被单引号所使用的撇号所迷惑)

代码语言:javascript
运行
复制
"""countwords.py
   count all words across all files
"""

import fileinput
import re
import collections

# create a regex delimiter that is any character that is  not 1 or
# more word character or an apostrophe, this allows contractions
# to be treated as a word (eg can't  won't  didn't )
# Caution: this WILL get confused by a line that uses apostrophe
# as a single quote: eg 'hello' would be treated as a 7 letter word

word_delimiter = re.compile(r"[^\w']+")

# create an empty Counter

counter = collections.Counter()

# use fileinput.input() to open and read ALL lines from ALL files
# specified on the command line, or if no files specified on the
# command line then read from STDIN (ie the keyboard or redirect)

for line in fileinput.input():
    for word in word_delimiter.split(line):
        counter[word.lower()] += 1   # count case insensitively

del counter['']   # handle corner case of the occasional 'empty' word

# compute the total number of words using .values() to get an
# generator of all the Counter values (ie the individual word counts)        
# then pass that generator to the sum function which is able to 
# work with a list or a generator

total = sum(counter.values())

# iterate through the key/value pairs (ie word/word_count) in sorted
# order - the lambda function says sort based on position 1 of each
# word/word_count tuple (ie the word_count) and reverse=True does
# exactly what it says = reverse the normal order so it now goes
# from highest word_count to lowest word_count

print("{:>10s}  {:>8s} {:s}".format("occurs", "percent", "word"))

for word, count in sorted(counter.items(),
                          key=lambda t: t[1],
                          reverse=True):
    print ("{:10d} {:8.2f}% {:s}".format(count, count/total*100, word))

示例输出:

代码语言:javascript
运行
复制
$ python3 countwords.py
I have a dog, he is a good dog, but he can't fly
^D

occurs   percent word
     2    15.38% a
     2    15.38% dog
     2    15.38% he
     1     7.69% i
     1     7.69% have
     1     7.69% is
     1     7.69% good
     1     7.69% but
     1     7.69% can't
     1     7.69% fly

和:

代码语言:javascript
运行
复制
$ python3 countwords.py text1 text2
    occurs   percent word
         2    11.11% hello
         2    11.11% i
         1     5.56% there
         1     5.56% how
         1     5.56% are
         1     5.56% you
         1     5.56% am
         1     5.56% fine
         1     5.56% mark
         1     5.56% where
         1     5.56% is
         1     5.56% the
         1     5.56% dog
         1     5.56% haven't
         1     5.56% seen
         1     5.56% him
票数 2
EN

Stack Overflow用户

发布于 2019-12-04 00:19:45

使用您的代码,下面是一种更整洁的方法:

代码语言:javascript
运行
复制
# Initializing Dictionary
d = {}
with open(sys.argv[1], 'r') as f:

    # counting number of times each word comes up in list of words (in dictionary)
    for line in f: 
        words = line.lower().split() 
        # Iterate over each word in line 
        for word in words: 
            if word not in d.keys():
                d[word] = 1
            else:
                d[word]+=1

n_all_words = sum([k.values])

# Print percentage occurance
for k, v in d.items():
    print(f'{k} occurs {v} times and is {(100*v/n_all_words):,.2f}% total of words.')


# Sort a dictionary using this useful solution
# https://stackoverflow.com/a/613218/10521959
import operator
sorted_d = sorted(d.items(), key=operator.itemgetter(1))
票数 1
EN

Stack Overflow用户

发布于 2019-12-04 02:02:47

正如注释中提到的,这正是collections.Counter

代码语言:javascript
运行
复制
words = 'a b c a'.split()
print(Counter(words).most_common())

来自docs:https://docs.python.org/2/library/collections.html

代码语言:javascript
运行
复制
most_common([n])
Return a list of the n most common elements and their counts
from the most common to the least. If n is omitted or None,
most_common() returns all elements in the counter.
Elements with equal counts are ordered arbitrarily:

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59167209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档