我想将元组列表中的两个字符串元素串联起来
我有这个:
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"]
答案 0 :(得分:1)
使用列表推导,对于两个元素,我将使用元组拆包和串联:
myanswer = [s1 + s2 for s1, s2 in mylist]
另一种选择是使用formatted string literal:
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()
方法,请参见What is the advantage of a list comprehension over a for loop?
格式化的字符串文字是在Python 3.6中引入的,很容易是用内插元素组成字符串的最快方法(即使它们didn't start out that way)。
我还尝试了[itertools.starmap()
和[operator.add()
]的[str.join()
],但这似乎没有竞争力:
>>> 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))
每增加100万个元素,则赢取的利润很少(133ms对带有串联的列表理解为140ms)。