繁体   English   中英

如何在列表中提取每个元组的'x'并以python方式连接?

[英]How to extract 'x' of each tuple in a list and concatenate in python way?

我有一个这样的值,我从NLTK树中提取。


[[('Happy', 'NNP'), ('Mother', 'NNP')], [('Day', 'NNP')], [('Joey', 'NNP'), ('M.', 'NNP'), ('Bing', 'NNP')], [('kind', 'NN')], [('happy', 'JJ'), ('wife', 'NN')], [('mother', 'NN')], [('friend', 'NN')]]

我希望最终的结果是

['Happy Mother','Day','Joey M. Bing','kind','happy wife','mother','friend']

我怎么用python方式做到这一点?

这是我到目前为止所做的,我知道这非常难看。 我是一个蟒蛇处女。


Y = []
for x in X:
    s = ""
    for z in x:
        s += z[0] + " "
    Y.append(s)

print Y

你可以使用zipstr.join轻松str.join

result = [' '.join(zip(*row)[0]) for row in data]

zip(*sequences)[i]是一种常见的Python习惯用法,用于从每个序列中获取第i个值(列表,元组等)

它类似于[seq[i] for seq in sequences]但即使序列不是可订阅的(例如迭代器),它也可以工作。 在Cpython中,由于使用了内置函数,它可能会稍快一些(尽管如果它很重要,你应该总是进行分析)。 此外,它返回一个元组而不是列表。

有关更多信息,请参阅文档

Y = [' '.join(t[0] for t in l) for l in X]

使用列表理解:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM