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!
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
.