如何在python列表中将文本转换为元组 [英] how to convert a text into tuples in a list in python

查看:746
本文介绍了如何在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.

这篇关于如何在python列表中将文本转换为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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