python计算单词个数_如何在Python句子中计算单词数?

python计算单词个数

python计算单词个数

I want to send my text to the python application as argument. The application will return the count of words in the text. Here is the basic but useful sample python application.

我想将我的文本作为参数发送到python应用程序。 应用程序将返回文本中的单词数。 这是基本但有用的示例python应用程序。

Python脚本 (Python Script)

We have following python script which is named sentence_word_count.py . It gets sentence as argument and split it according to spaces. Uses len() function in order to count elements in the given list.

我们有以下python脚本,名称为sentence_word_count.py 。 它获取句子作为自变量并根据空格将其拆分。 使用len()函数以计算给定列表中的元素。

import sys 
 
def countwords(s): 
    count=len(s.split()) 
    return count 
 
print("Sentence:") 
print(sys.argv[1]) 
print("Total Word Count:") 
print(countwords(sys.argv[1]))
Python Script 
Python Script
Python脚本

运行Python脚本

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值