[英]How to extract 'x' of each tuple in a list and concatenate in python way?
I have a value like this which I extract from NLTK tree. 我有一个这样的值,我从NLTK树中提取。
[[('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? 我怎么用python方式做到这一点?
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
. 你可以使用zip
和str.join
轻松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.) zip(*sequences)[i]
是一种常见的Python习惯用法,用于从每个序列中获取第i个值(列表,元组等)
It is similar to [seq[i] for seq in sequences]
but it works even if the sequences are not subscriptable (for example iterators). 它类似于[seq[i] for seq in sequences]
但即使序列不是可订阅的(例如迭代器),它也可以工作。 In Cpython, it may be slightly faster due to using a builtin (though you should always profile if it's important). 在Cpython中,由于使用了内置函数,它可能会稍快一些(尽管如果它很重要,你应该总是进行分析)。 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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.