如何找到列表中最常见的元素?[重复]

发布于 2021-01-29 14:10:34

这个问题已经在这里有了答案

在列表中找到出现次数最多的项目 (13个答案)

4个月前关闭。

给出以下列表

['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 
 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 
 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 
 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 
 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 
 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 
 'Moon', 'to', 'rise.', '']

我正在尝试计算每个单词出现多少次并显示前3位。

但是,我只想查找首字母大写的前三位,而忽略不首字母大写的所有单词。

我敢肯定有比这更好的方法,但是我的想法是做以下事情:

  1. 将列表中的第一个单词放入另一个称为uniquewords的列表中
  2. 从原始列表中删除第一个单词及其所有重复单词
  3. 将新的第一个单词添加到唯一单词中
  4. 从原始列表中删除第一个单词及其所有重复单词。
  5. 等等…
  6. 直到原始列表为空。
  7. 计算唯一单词中每个单词出现在原始列表中的次数
  8. 找到前三名并打印
关注者
0
被浏览
134
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    如果您使用的是Python的早期版本,或者您有充分的理由推出自己的单词计数器(我想听听它!),则可以尝试使用以下方法dict

    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> word_list = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', '']
    >>> word_counter = {}
    >>> for word in word_list:
    ...     if word in word_counter:
    ...         word_counter[word] += 1
    ...     else:
    ...         word_counter[word] = 1
    ... 
    >>> popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
    >>> 
    >>> top_3 = popular_words[:3]
    >>> 
    >>> top_3
    ['Jellicle', 'Cats', 'and']
    

    热门提示 :每当您要使用这样的算法时,交互式Python解释器就是您的朋友。只需将其键入并观看,然后检查整个过程中的元素。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看