繁体   English   中英

在python的列表中连接元组的元素

[英]Concatenate elements of a tuple in a list in python

我有一个元组列表,其中包含字符串例如:

[('this', 'is', 'a', 'foo', 'bar', 'sentences')
('is', 'a', 'foo', 'bar', 'sentences', 'and')
('a', 'foo', 'bar', 'sentences', 'and', 'i')
('foo', 'bar', 'sentences', 'and', 'i', 'want')
('bar', 'sentences', 'and', 'i', 'want', 'to')
('sentences', 'and', 'i', 'want', 'to', 'ngramize')
('and', 'i', 'want', 'to', 'ngramize', 'it')]

现在我希望连接一个元组中的每个字符串以创建一个空格分隔字符串列表。 我使用以下方法:

NewData=[]
for grams in sixgrams:
       NewData.append( (''.join([w+' ' for w in grams])).strip())

哪个工作得很好。

但是,我拥有的列表有超过一百万个元组。 所以我的问题是这种方法足够有效还是有更好的方法来做到这一点。 谢谢。

对于大量数据,您应该考虑是否需要将其全部保存在列表中。 如果你一次处理每一个,你可以创建一个生成器,它将产生每个连接的字符串,但不会让它们全部占用内存:

new_data = (' '.join(w) for w in sixgrams)

如果你也可以从生成器获得原始元组,那么你也可以避免在内存中使用sixgrams列表。

列表理解创建临时字符串。 只需使用' '.join

>>> words_list = [('this', 'is', 'a', 'foo', 'bar', 'sentences'),
...               ('is', 'a', 'foo', 'bar', 'sentences', 'and'),
...               ('a', 'foo', 'bar', 'sentences', 'and', 'i'),
...               ('foo', 'bar', 'sentences', 'and', 'i', 'want'),
...               ('bar', 'sentences', 'and', 'i', 'want', 'to'),
...               ('sentences', 'and', 'i', 'want', 'to', 'ngramize'),
...               ('and', 'i', 'want', 'to', 'ngramize', 'it')]
>>> new_list = []
>>> for words in words_list:
...     new_list.append(' '.join(words)) # <---------------
... 
>>> new_list
['this is a foo bar sentences', 
 'is a foo bar sentences and', 
 'a foo bar sentences and i', 
 'foo bar sentences and i want', 
 'bar sentences and i want to', 
 'sentences and i want to ngramize', 
 'and i want to ngramize it']

以上for循环可表示为以下列表理解:

new_list = [' '.join(words) for words in words_list] 

你可以像这样有效地做到这一点

joiner = " ".join
print map(joiner, sixgrams)

我们仍然可以使用这样的列表理解来提高性能

joiner = " ".join
print [joiner(words) for words in sixgrams]

性能对比表明,上面列出的列表理解解决方案比其他两个解决方案略快。

from timeit import timeit

joiner = " ".join

def mapSolution():
    return map(joiner, sixgrams)

def comprehensionSolution1():
    return ["".join(words) for words in sixgrams]

def comprehensionSolution2():
    return [joiner(words) for words in sixgrams]

print timeit("mapSolution()", "from __main__ import joiner, mapSolution, sixgrams")
print timeit("comprehensionSolution1()", "from __main__ import sixgrams, comprehensionSolution1, joiner")
print timeit("comprehensionSolution2()", "from __main__ import sixgrams, comprehensionSolution2, joiner")

在我的机器上输出

1.5691678524
1.66710209846
1.47555398941

性能提升很可能是因为我们不必每次都从空字符串创建连接函数。

编辑:虽然我们可以改善这样的性能,但最流行的方式是使用lvc的答案中的生成器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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