简体   繁体   English

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

[英]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 . 你可以使用zipstr.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.

相关问题 在python的列表中连接元组的元素 - Concatenate elements of a tuple in a list in python 在Python列表中为每个元组的特定位置提取变量 - Extract variable at specific position for each tuple in a Python list 如何将每对列表转换为 python 中的元组 - How to convert each pair of list into tuple in python 为python中的列表/元组中的每个数据调用API的最佳方法是什么? - What is the best way to call API for each data in the list/tuple in python? 在Python 3x中对列表/元组执行计算的最佳方法 - Best way to perform calculations on a list/tuple in Python 3x Python:对于列表中的每个元组,检查元组中是否包含字符串 - Python: for each tuple in list, check if string in tuple Python - 如何从列表中提取元组? - Python - how do I extract a tuple from a list? Python:有没有办法提取和连接多个文本文件系列,将每个文件的前 3 行作为我 go 一起删除? - Python: Is there a way to extract and concatenate several series of text files, dropping the top 3 rows of each file as I go along? 在Python中有一个元组列表,并且对于每个元组,都希望将x [0]和x [1]放入Excel电子表格的A和B列 - In Python have a list of tuples and for each tuple would like to put x[0] and x[1] into column A and B of an Excel spreadsheet 如何在列表/ python中获取每个元组的第一项 - How to get first item of each tuple in a list / python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM