问题
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