I have a value like this which I extract from NLTK tree.
[[('Happy', 'NNP'), ('Mother', 'NNP')], [('Day', 'NNP')], [('Joey', 'NNP'), ('M.', 'NNP'), ('Bing', 'NNP')], [('kind', 'NN')], [('happy', 'JJ'), ('wife', 'NN')], [('mother', 'NN')], [('friend', 'NN')]]
I would like the end result to be
['Happy Mother','Day','Joey M. Bing','kind','happy wife','mother','friend']
How do I do this in python way?
This is what I have done so far, which is very ugly I know. I'm a python virgin.
Y = []
for x in X:
s = ""
for z in x:
s += z[0] + " "
Y.append(s)
print Y
You can do it pretty easily with zip
and str.join
.
result = [' '.join(zip(*row)[0]) for row in data]
zip(*sequences)[i]
is a common Python idiom for getting the ith value from each sequence (list, tuple, etc.)
It is similar to [seq[i] for seq in sequences]
but it works even if the sequences are not subscriptable (for example iterators). In Cpython, it may be slightly faster due to using a builtin (though you should always profile if it's important). Also, it returns a tuple instead of a list.
For more information, see the documentation .
Y = [' '.join(t[0] for t in l) for l in X]
Use a list comprehension:
>>> X = [[('Happy', 'NNP'), ('Mother', 'NNP')], [('Day', 'NNP')], [('Joey', 'NNP'), ('M.', 'NNP'), ('Bing', 'NNP')], [('kind', 'NN')], [('happy', 'JJ'), ('wife', 'NN')], [('mother', 'NN')], [('friend', 'NN')]]
>>> Y = [' '.join(z[0] for z in x) for x in X]
>>> Y
['Happy Mother', 'Day', 'Joey M. Bing', 'kind', 'happy wife', 'mother', 'friend']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.