繁体   English   中英

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

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

我想串联两个元组列表中的两个字符串元素

我有这个:

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

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

它正在工作,但是有没有简单的方法可以做到这一点? 我的实际列表中大约有1000项,我认为for循环不是最有效的方法。

预期产量:

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

使用列表推导,对于两个元素,我将使用元组拆包和串联:

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

另一种选择是使用格式化的字符串文字

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

两者都相当快:

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

串联在这里胜出。

列表理解无需每次在循环中查找列表对象及其.append()方法,请参阅列表理解与for循环相比有什么优势?

格式化的字符串文字是在Python 3.6中引入的,并且很容易是用内插元素组成字符串的最快方法(即使它们不是以这种方式开始的 )。

我也用[ 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

它的确增加了更多元素; 每增加一百万个元素, map(starmap(add, largelist))的获胜率就很小(133ms对带有串联的列表理解为140ms)。

暂无
暂无

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

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