本文介绍了Python将多个单词的列表转换为单个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词列表,例如:

I have a list of words for example:

words = ['one','two','three four','five','six seven']#引号缺失

我正在尝试创建一个新列表,其中列表中的每个项目都只是一个字,所以我应该:

And I am trying to create a new list where each item in the list is just one word so I would have:

words = ['one','two','three','four','five','six','seven']

最好的方法是将整个列表连接到一个字符串中,然后对该字符串进行标记化吗?像这样:

Would the best thing to do be join the entire list into a string and then tokenize the string? Something like this:

word_string = ' '.join(words)tokenize_list = nltk.tokenize(word_string)

word_string = ' '.join(words)tokenize_list = nltk.tokenize(word_string)

还是有更好的选择?

推荐答案

您可以使用空格分隔符加入,然后再次拆分:

You can join using a space separator and then split again:

In [22]:

words = ['one','two','three four','five','six seven']
' '.join(words).split()
Out[22]:
['one', 'two', 'three', 'four', 'five', 'six', 'seven']

这篇关于Python将多个单词的列表转换为单个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 14:52