将两个列表连接成一个格式化的字符串最聪明的方法 [英] smartest way to join two lists into a formatted string
问题描述
可以说我有两个相同长度的列表:
a = ['a1','a2','a3 ']
b = ['b1','b2','b3']
和我想产生下面的字符串:
$ $ $ $ $ $ $ $ c $'a1 = b1,a2 = b2,a3 = b3'
达到这个目的的最好方法是什么?
我有以下的实现:
pre $ import timeit
a = [str(f)for f in range 500)]
b = [str(f)for f in range(500)]
def func1():
return','.join([aa +'='如果a.index(aa)== b.index(bb)])
def func2():
list = []
在范围内(len(a)):
list.append('%s =%s'%(a [i],b [i]))
return','。 join(list)
$ b $ = timeit.Timer(从__main__导入func1的设置=',stmt ='func1()')
print'func1 ='+ t.timeit(10)
t = timeit.Timer(setup ='from __main__ import func2',stmt ='func2()')
print'func2 ='+ t.timeit(10)
,输出结果为:
$ p $ code func1 = 32.4704790115
func2 = 0.00529003143311
您有一些权衡吗?
a = ['a1','a2','a3']
b = ['b1','b2','b3']
pat ='%s = %% s,%s = %% s,%s = %% s'
print pat%tuple(a)%tuple b)
给出 a1 = b1,a2 = b2,a3 = b3
。
然后:
from timeit import Timer $ b $ from itertools import izip
n = 300
a = [str(f)for (n)]
b = [str(f)for f in range(n)]
def func1():
return','.join([如果a.index(aa)== b.index(bb)])
def func2():
list aa +'='+ bb for aa in a for bb in b []
我在范围内(len(a)):
list.append('%s =%s'%(a [i],b [i]))
return','.join(list)
$ b $ def func3
return','.join('%s =%s'%t for zip(a,b))
$ b $ def func4():
return', '.join('%s =%s'%t for izip(a,b))
def func5():
pat = n *'%s = %% (b)b
$ bd = dict(zip((1,2,3,4,5),('heavy','')
return pat%tuple(a)%tuple (1,6):
t = Timer(setup ='from __main__ import func%d'% i,stmt ='func%d()'%i)
print'func%d =%s%s'%(i,t.timeit(10),d [i])
结果
func1 = 16.2272833558 heavy
func2 = 0.00410247671143 append
func3 = 0.00349569568199 zip
func4 = 0.00301686387516 izip
func5 = 0.00157338432678%格式化
Lets say I have two lists of same length:
a = ['a1', 'a2', 'a3']
b = ['b1', 'b2', 'b3']
and I want to produce the following string:
c = 'a1=b1, a2=b2, a3=b3'
What is the best way to achieve this?
I have following implementations:
import timeit
a = [str(f) for f in range(500)]
b = [str(f) for f in range(500)]
def func1():
return ', '.join([aa+'='+bb for aa in a for bb in b if a.index(aa) == b.index(bb)])
def func2():
list = []
for i in range(len(a)):
list.append('%s=%s' % (a[i], b[i]))
return ', '.join(list)
t = timeit.Timer(setup='from __main__ import func1', stmt='func1()')
print 'func1 = ' + t.timeit(10)
t = timeit.Timer(setup='from __main__ import func2', stmt='func2()')
print 'func2 = ' + t.timeit(10)
and the output is:
func1 = 32.4704790115
func2 = 0.00529003143311
Do you have some trade-off?
a = ['a1', 'a2', 'a3']
b = ['b1', 'b2', 'b3']
pat = '%s=%%s, %s=%%s, %s=%%s'
print pat % tuple(a) % tuple(b)
gives a1=b1, a2=b2, a3=b3
.
Then:
from timeit import Timer
from itertools import izip
n = 300
a = [str(f) for f in range(n)]
b = [str(f) for f in range(n)]
def func1():
return ', '.join([aa+'='+bb for aa in a for bb in b if a.index(aa) == b.index(bb)])
def func2():
list = []
for i in range(len(a)):
list.append('%s=%s' % (a[i], b[i]))
return ', '.join(list)
def func3():
return ', '.join('%s=%s' % t for t in zip(a, b))
def func4():
return ', '.join('%s=%s' % t for t in izip(a, b))
def func5():
pat = n * '%s=%%s, '
return pat % tuple(a) % tuple(b)
d = dict(zip((1,2,3,4,5),('heavy','append','zip','izip','% formatting')))
for i in xrange(1,6):
t = Timer(setup='from __main__ import func%d'%i, stmt='func%d()'%i)
print 'func%d = %s %s' % (i,t.timeit(10),d[i])
result
func1 = 16.2272833558 heavy
func2 = 0.00410247671143 append
func3 = 0.00349569568199 zip
func4 = 0.00301686387516 izip
func5 = 0.00157338432678 % formatting
这篇关于将两个列表连接成一个格式化的字符串最聪明的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!