相关疑难解决方法(0)

如何找到列表中最常见的元素?

鉴于以下列表

['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.', '']
Run Code Online (Sandbox Code Playgroud)

我试图计算每个单词出现的次数并显示前3个.

但是我只想找到第一个字母大写的前三个,并忽略所有没有首字母大写的单词.

我相信有比这更好的方法,但我的想法是做以下事情:

  1. 将列表中的第一个单词放入另一个名为uniquewords的列表中
  2. 从原始列表中删除第一个单词及其复制的所有单词
  3. 将新的第一个单词添加到唯一的单词中
  4. 删除第一个单词及其原始列表中的所有单词.
  5. 等等...
  6. 直到原始列表为空....
  7. 计算唯一字中每个单词在原始列表中出现的次数
  8. 找到前三名并打印

python list frequency

32
推荐指数
7
解决办法
9万
查看次数

标签 统计

frequency ×1

list ×1

python ×1