将2d字符列表加入字符串

时间:2015-03-08 18:53:33

标签: python list-comprehension

假设loc是一个2d字符列表,我想将它加入到字符串s中。以下哪个代码更快?

s = ''.join([''.join(row) for row in loc])

s = ''.join([x for row in loc for x in row])

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以使用timeit模块查看时差。

import timeit

loc = [('a', 'b'),('c', 'd'), ('e', 'f')]

s1 = lambda: ''.join([''.join(row) for row in loc])    
s2 = lambda: ''.join([x for row in loc for x in row])

print 'Time required by s1:', timeit.timeit(s1)
print 'Time required by s2:', timeit.timeit(s2)

将所需时间结果表示为:

Time required by s1: 1.29390706928
Time required by s2: 1.46863953561

说明:

1)对于 s1 [''.join(row) for row in loc]需要3 (for row in loc) * 2 (''.join(row)) = 6次迭代        我们得到['ab', 'cd', 'ef']作为最外层join函数的输入,这又需要3次迭代。 因此,s1所需的总迭代次数为6 + 3 = 9

2)对于 s2 [x for row in loc for x in row]需要3 (for row in loc) * 2 (for x in row) = 6次迭代        我们得到['a', 'b', 'c', 'd', 'e', 'f']作为最外层join函数的输入,需要6次迭代。 因此,s2所需的迭代总数为6 + 6 = 12

因此,结果 s1 所花费的时间少于 s2

答案 1 :(得分:2)

以下是您提到的两种方法以及使用itertools.chain.from_iterable

的时间结果
loc = np.random.randint(97, 123, (100, 100)).view('U2').tolist()

timeit.timeit("''.join([''.join(row) for row in loc])", setup="from __main__ import loc", number=10000)
1.1168044869991718

timeit.timeit("''.join([x for row in loc for x in row])", setup="from __main__ import loc", number=10000)
4.264987408001616

timeit.timeit("''.join(itertools.chain.from_iterable(loc))", setup="from __main__ import loc", number=10000)
2.068879542996001