how to convert a text into tuples in a list in python

て烟熏妆下的殇ゞ 提交于 2019-12-13 08:08:10

问题


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

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

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!


回答1:


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)

tuple() is used to transform each sub-item (which contains two elements) to a tuple.




回答2:


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')]



来源:https://stackoverflow.com/questions/41259747/how-to-convert-a-text-into-tuples-in-a-list-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!