我正在尝试计算txt文件中的所有字母,然后按降序显示

And*_*rew 6 python dictionary python-3.x display

正如标题所说:

到目前为止,这是我在我的代码工作的地方,但我无法按顺序显示信息.目前它只是随机显示信息.

def frequencies(filename):
    infile=open(filename, 'r')
    wordcount={}
    content = infile.read()
    infile.close()
    counter = {}
    invalid = "‘'`,.?!:;-_\n—' '"

    for word in content:
        word = content.lower()
        for letter in word:
            if letter not in invalid:
                if letter not in counter:
                    counter[letter] = content.count(letter)
                    print('{:8} appears {} times.'.format(letter, counter[letter]))
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

Kas*_*mvd 5

字典是无序的数据结构.此外,如果你想计算一组数据中的一些项目,你最好使用collections.Counter()哪个更优化和pythonic为这个目标.

然后您可以使用Counter.most_common(N)它来打印NCounter对象中最常见的项目.

另外,关于文件的打开,您可以简单地使用with在块结束时自动关闭文件的语句.最好不要在函数内打印最终结果,你可以通过产生预期的线条然后在你想要的时候打印它们来使你的函数成为生成器.

from collections import Counter

def frequencies(filename, top_n):
    with open(filename) as infile:
        content = infile.read()
    invalid = "‘'`,.?!:;-_\n—' '"
    counter = Counter(filter(lambda x: not invalid.__contains__(x), content))
    for letter, count in counter.most_common(top_n):
        yield '{:8} appears {} times.'.format(letter, count)
Run Code Online (Sandbox Code Playgroud)

然后使用for循环来迭代生成器函数:

for line in frequencies(filename, 100):
    print(line)
Run Code Online (Sandbox Code Playgroud)


MSe*_*ert 1

按降序显示需要在搜索循环之外,否则它们将在遇到时显示。

\n\n

使用内置函数按降序排序非常容易sorted(您需要设置 -reverse参数!)

\n\n

然而,Python包含电池,并且已经有一个Counter. 所以它可以很简单:

\n\n
from collections import Counter\nfrom operator import itemgetter\n\ndef frequencies(filename):\n    # Sets are especially optimized for fast lookups so this will be\n    # a perfect fit for the invalid characters.\n    invalid = set("\xe2\x80\x98\'`,.?!:;-_\\n\xe2\x80\x94\' \'")\n\n    # Using open in a with block makes sure the file is closed afterwards.\n    with open(filename, \'r\') as infile:  \n        # The "char for char ...." is a conditional generator expression\n        # that feeds all characters to the counter that are not invalid.\n        counter = Counter(char for char in infile.read().lower() if char not in invalid)\n\n    # If you want to display the values:\n    for char, charcount in sorted(counter.items(), key=itemgetter(1), reverse=True):\n        print(char, charcount)\n
Run Code Online (Sandbox Code Playgroud)\n\n

计数器已经有一个most_common方法,但您想要显示所有字符和计数,因此它不太适合这种情况。但是,如果您只想知道 x 个最常见的计数,那么它会合适。

\n