简体   繁体   English

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

[英]how to convert a text into tuples in a list in python

I am a beginner in python and desperately need someone's help. 我是python的初学者,非常需要别人的帮助。

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: 原始文本已被标记化,每个pos的标记如下:

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

And the desired output looks as below: 所需的输出如下所示:

[('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() 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) split()第一次使用空格分隔符形成字符串列表,第二次使用斜杠 (将每个子项目拆分为两个元素)

tuple() is used to transform each sub-item (which contains two elements) to a tuple . tuple()用于将每个子项目(包含两个元素)转换为一个tuple

x="The/DT Fulton/NNP County/NNP Grand/NNP Jury/NNP said/VBD Friday/NNP an/DT"
print re.findall(r"(\w+)\/(\w+)", x)

Output: 输出:

[('The', 'DT'), ('Fulton', 'NNP'), ('County', 'NNP'), ('Grand', 'NNP'), ('Jury', 'NNP'), ('said', 'VBD'), ('Friday', 'NNP'), ('an', 'DT')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM