如何在python列表中将文本转换为元组

如何在python列表中将文本转换为元组

问题描述:

我是python的初学者,非常需要别人的帮助.

I am a beginner in python and desperately need someone's help.

我正在尝试将文本转换为列表中的元组. 原始文本已被标记化,每个pos的标记如下:

I am trying to convert a text into tuples in a list. The original text was already tokenized and each pos was tagged as below:

The/DT Fulton/NNP County/NNP Grand/NNP Jury/NNP said/VBD Friday/NNP an/DT

所需的输出如下所示:

[('The', 'DT'), ('Fulton', 'NNP'), ('County', 'NNP'), ...)]

因此,如果有人可以为我提供帮助,那就太好了! 预先感谢!

So, if anyone can offer me a help, it would be so awesome! Thanks in advance!

您可以使用

You can use list comprehension like below:

>>> s = 'The/DT Fulton/NNP County/NNP Grand/NNP Jury/NNP said/VBD Friday/NNP an/DT'
>>> 
>>> [tuple(i.split('/')) for i in s.split()]
[('The', 'DT'), ('Fulton', 'NNP'), ('County', 'NNP'), ('Grand', 'NNP'), ('Jury', 'NNP'), ('said', 'VBD'), ('Friday', 'NNP'), ('an', 'DT')]

split() 用于形成列表第一次使用空格分隔符,第二次使用斜线分隔字符串(将每个子项分为两个元素)

split() is used to form a list of strings using space separator in the first time and slash in the second time (to split each sub-item to two elements)

tuple() 用于转换每个子-item(包含两个元素)到tuple.