拆分功能将句子分成单词 [英] Split function to split sentence into words

查看:163
本文介绍了拆分功能将句子分成单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我没有足够的经验来编写Python代码,但现在我想看看我如何开始使用Python。

我试着写一个可以显示句子的简单程序。现在我的问题是如何使用split函数编写代码将该句子分成单词然后分别打印出每个单词。让我举个例子:


>>> sentence ="我的问题是要知道如何用Python编写代码


然后这个句子的输出必须给出:


句子[1] =我的

句子[2] =问题

句子[3] =是

句子[4] =到

句子[5] =知道

......

.......


可以有人帮助我吗?

解决方案

请务必查看文档,了解有趣的内容。在这种情况下,请查看可能的字符串方法( http://www.python.org/doc/2.5.2/lib/string-methods.html )您将看到分割功能。这是一个简单的例子。

展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号


感谢您的帮助,但问题没有得到完全解答!使用这个程序它将分割句子,但我想输出是让我们说如果我们有一个=杰克吃苹果,我希望输出为:

a [0] =杰克

a [1] =吃了

a [2] =

a [3] =苹果


你可以看看是否有可能获得上述输出吗?



感谢您的帮助,但问题没有得到完全解答!使用这个程序它将分割句子,但我想输出是让我们说如果我们有一个=杰克吃苹果,我希望输出为:

a [0] =杰克

a [1] =吃了

a [2] =

a [3] =苹果


你能否看看它是否有可能获得上述产量?



答案是:字符串格式化!


示例:

展开 | 选择 | Wrap | 行号< /跨度>

Hi,

i don''t have enough experience in writing codes in Python but now i''m trying to see how i can start using Python.
I''ve tried to write a simple program that can display a sentence. now my problem is how to write a code using split function to split that sentence into words then print out each word separately. let me give u an example:

>>>sentence=" My question is to know how to write a code in Python"

then the output of this sentece must give:

sentence[1]=My
sentence[2]=question
sentence[3]=is
sentence[4]=to
sentence[5]=know
......
.......

Can someone help me in this?

解决方案

Always check the documentation, for something interesting. In this case, look at possible string methods ( http://www.python.org/doc/2.5.2/lib/string-methods.html ) and you will see a split function. Here?s a quick example.

Expand|Select|Wrap|Line Numbers


thank you for the help but the question is not fully answered! with this program it will split the sentence but i would like the output to be lets say if we have a= jack ate the apple, i would like the output to be:
a[0]=jack
a[1]=ate
a[2]=the
a[3]=apple

can you please see if its possible to get the above output?


thank you for the help but the question is not fully answered! with this program it will split the sentence but i would like the output to be lets say if we have a= jack ate the apple, i would like the output to be:
a[0]=jack
a[1]=ate
a[2]=the
a[3]=apple

can you please see if its possible to get the above output?

The answer is: string formatting!

Example:

Expand|Select|Wrap|Line Numbers


这篇关于拆分功能将句子分成单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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