
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脚本(Run Python Script)
We will use sentence_word_count.py
script in order to count given sentence word count. In this case sentence is This is a sample sentence
.
我们将使用sentence_word_count.py
_单词_计数sentence_word_count.py
脚本来计算给定句子的字数。 在这种情况下,句子为。 This is a sample sentence
。
$ python sentence_word_count.py "This is a sample sentence"
Sentence:
This is a sample sentence
Total Word Count:
5
python计算单词个数