Python-计算单词列表中的每个字母 [英] Python- Count each letter in a list of words

查看:518
本文介绍了Python-计算单词列表中的每个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个单词列表'wordList = list().现在,我正在使用此代码对整个列表中每个单词的每个字母进行计数

So I have a list of words `wordList = list().' Right now, I am counting each letter in each of the words throughout the whole list using this code

cnt = Counter()
for words in wordList:
      for letters in words:
          cnt[letters]+=1

但是,我希望它的计数方式有所不同.我希望函数在列表中的所有单词中找到最常见的字母,但仅对每个单词计算一次即可(忽略某些单词可以具有同一字母的多个副本这一事实).

However, I want it to count differently. I want the function to find the most common letter out of all the words in the list, but only by counting each letter per word once (ignoring the fact that some words can have multiple copies of the same letter).

例如,如果列表中包含快乐,竖琴和草率",则快乐中的两个p应当只计算一次.因此,该函数应返回频率最高的字母列表(按顺序),而无需重复计算.在上述情况下,它为'h,a,p,y,r,s"

For example, if the list contained 'happy, harpy and hasty', the two p's in happy should only be counted once. So the function should return a list of the highest frequency letters (in order) without double counting. In the above case it would be 'h, a, p, y, r, s"

推荐答案

cnt = Counter()
for words in wordList:
      for letters in set(words):
          cnt[letters]+=1

这篇关于Python-计算单词列表中的每个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆