简体   繁体   English

从列表中的元组有效地连接两个字符串?

[英]Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuples 我想串联两个元组列表中的两个字符串元素

I have this: 我有这个:

mylist = [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]
myanswer = []

for tup1 in mylist:
   myanswer.append(tup1[0] + tup[1])

It's working but is there any easy way to do this? 它正在工作,但是有没有简单的方法可以做到这一点? My real list has around 1000 items and I don't think a for loop is the most efficient way. 我的实际列表中大约有1000项,我认为for循环不是最有效的方法。

Expected output: 预期产量:

myanswer = ["ab", "cd", "ef", "gh"]

Use a list comprehension, and for just two elements, I'd use tuple unpacking and concatenation: 使用列表推导,对于两个元素,我将使用元组拆包和串联:

myanswer = [s1 + s2 for s1, s2 in mylist]

Another option is to use a formatted string literal : 另一种选择是使用格式化的字符串文字

myanswer = [f"{s1}{s2}" for s1, s2 in mylist]

Both are reasonably fast: 两者都相当快:

>>> from random import choice
>>> from string import ascii_letters
>>> from timeit import Timer
>>> testdata = [(choice(ascii_letters), choice(ascii_letters)) for _ in range(10000)]
>>> count, total = Timer('[f"{s1}{s2}" for s1, s2 in mylist]', 'from __main__ import testdata as mylist').autorange()
>>> print(f"List comp with f-string, 10k elements: {total / count * 1000000:7.2f} microseconds")
List comp with f-string, 10k elements: 1249.37 microseconds
>>> count, total = Timer('[s1 + s2 for s1, s2 in mylist]', 'from __main__ import testdata as mylist').autorange()
>>> print(f"List comp with concatenation, 10k elements: {total / count * 1000000:6.2f} microseconds")
List comp with concatenation, 10k elements: 1061.89 microseconds

Concatenation wins out here. 串联在这里胜出。

A list comprehension removes the need to look up the list object and its .append() method each time in a loop, see What is the advantage of a list comprehension over a for loop? 列表理解无需每次在循环中查找列表对象及其.append()方法,请参阅列表理解与for循环相比有什么优势?

Formatted string literals where introduced in Python 3.6, and are easily the fastest way of composing strings with interpolated elements (even though they didn't start out that way ). 格式化的字符串文字是在Python 3.6中引入的,并且很容易是用内插元素组成字符串的最快方法(即使它们不是以这种方式开始的 )。

I also tried out [ itertools.starmap() ] with [ operator.add() ] and [ str.join() ], but this doesn't appear to be competitive: 我也用[ operator.add() ]和[ str.join() ]尝试了[ itertools.starmap() ],但这似乎没有竞争力:

>>> count, total = Timer('list(starmap(add, mylist))', 'from __main__ import testdata as mylist; from itertools import starmap; from operator import add').autorange()
>>> print(f"itertools.starmap and operator.add, 10k elements: {total / count * 1000000:6.2f} microseconds")
itertools.starmap and operator.add, 10k elements: 1275.02 microseconds
>>> count, total = Timer('list(starmap(str.join, mylist))', 'from __main__ import testdata as mylist; from itertools import starmap').autorange()
>>> print(f"itertools.starmap and str.join, 10k elements: {total / count * 1000000:6.2f} microseconds")
itertools.starmap and str.join, 10k elements: 1564.79 microseconds

It does improve with more elements; 它的确增加了更多元素; by 1 million elements, map(starmap(add, largelist)) is winning by a small margin (133ms vs 140ms for a list comprehension with concatenation). 每增加一百万个元素, map(starmap(add, largelist))的获胜率就很小(133ms对带有串联的列表理解为140ms)。

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

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