计算每个句子的平均单词数

编程入门 行业动态 更新时间:2024-10-28 03:30:35
本文介绍了计算每个句子的平均单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难计算每个句子的单词数.就我而言,我假设句子仅以!" ,?" 或." 结尾

我有一个看起来像这样的列表:

[嘿,"!,"如何,"是,"你,"?,"我,"会,"喜欢,"一个,"三明治,."]

对于上面的示例,计算将为 1 + 3 + 5/3 .不过,我很难做到这一点!有什么想法吗?

解决方案

一个简单的解决方案:

mylist = [嘿",!",如何",是",您",?",我",会",喜欢",一个",三明治", "."]terminal = set([.",?",!"])#集对于成员资格"测试非常有效terminal_count = 0对于mylist中的项目:如果终端中有项目:#这是我们的会员资格测试terminal_count + = 1avg =(len(mylist)-terminal_count)/float(terminal_count)

这假设您只关心获得平均值,而不是每个句子的单个计数.

如果想花点时间,可以将 for 循环替换为以下内容:

terminal_count = sum(如果终端中有项目,则mylist中的项目为1)

I'm having a bit of trouble trying to count the number of words per sentence. For my case, I'm assuming sentences only end with either "!", "?", or "."

I have a list that looks like this:

["Hey, "!", "How", "are", "you", "?", "I", "would", "like", "a", "sandwich", "."]

For the example above, the calculation would be 1 + 3 + 5 / 3. I'm having a hard time achieving this, though! Any ideas?

解决方案

A simple solution:

mylist = ["Hey", "!", "How", "are", "you", "?", "I", "would", "like", "a", "sandwich", "."] terminals = set([".", "?", "!"]) # sets are efficient for "membership" tests terminal_count = 0 for item in mylist: if item in terminals: # here is our membership test terminal_count += 1 avg = (len(mylist) - terminal_count) / float(terminal_count)

This assumes you only care about getting the average, not the individual counts per sentence.

If you'd like to get a little fancy, you can replace the for loop with something like this:

terminal_count = sum(1 for item in mylist if item in terminals)

更多推荐

计算每个句子的平均单词数

本文发布于:2023-11-12 02:53:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词数   句子   平均

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!