从列表中的元组有效地连接两个字符串? [英] Efficiently concatenate two strings from tuples in a list?

查看:238
本文介绍了从列表中的元组有效地连接两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to concatenate the two string elements in a list of tuples

我有这个:

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

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

它正在工作,但是有什么简单的方法吗?我的实际列表中大约有1000项,我认为for循环不是最有效的方法.

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.

预期输出:

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]

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

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()方法,请参见

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?

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

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

我也用[operator.add()]和[str.join()]尝试了[itertools.starmap()],但这似乎没有竞争力:

I also tried out [itertools.starmap()] with [operator.add()] and [str.join()], but this doesn't appear to be competitive:

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

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

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

这篇关于从列表中的元组有效地连接两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆