python - smartest way to join two lists into a formatted string -
Let me say that I have two lists of the same length:
a = ['A1', 'A2', 'a3'] b = ['b1', 'b2', 'b3'] and I want to create the following string: / P>
c = 'a1 = b1, a2 = b2, a3 = b3' What is the best way to get it?
Declination (500)] B = [F (F) Category for F (500)] DEF func1 (): Return ',' (AA + 'A' in B in B in A for B in a.index (AA) == B.index (BB)] DEF func2 (): List = [] In the range I (Lane (A)) : List.append ('% s =% s'% (a [i], b [i]) returns ',' .join (list) t = timeit.Timer (setup = '__main__ import func1', stmt = 'Func1 ()') print 'func1 =' + t.timeit (10) t = timeit.Timer (setup = 'to __main__ import func2', stmt = 'func2 ()') Print 'Func2 =' + t Timeit (10)
and output is:
func1 = 32.4704790115 func2 = 0.00529003143311 Do you have anything Is trade-off?
a = ['a1', 'a2', 'a3 '] B = [' B1 '' B2 ',' B3 '] ='% s = %% S,% s = %% s,% s = %% s Print Print% Tupl (A)% Tupl (B) returns a1 = b1, a2 = b2 , A3 = b3 . Then:
timer import from timer with imports of time izip n = 300 a = [ F (n)] for the range (f) for category (f)] b = [fr (f)] def func1 (): returns' '.' (): (For category) : () = (): List = [] for I (AA + '=' + for BBB in B. if a.index (aa) == B. andx (BB)]) DEF func2 () line (A): list.append ('% s =% s'% (a [i], b [i]) returns ',' .join (list) def func3 (): and Exclamation ','. (A, B) for z ('% s =% s'% t) def func4 (): Return ','. ('% S =% s'% t for T in Iz a, A, B)) def func5 (): pat = n * '% s = %% s,' return pattern% tuple (a)% tupl B) D = dec (zip ((1,2,3,4,5)), ('izip', '% formatting' in xrange for 'heavy', 'attachment', 'zip', i))) (1,6): '__main__ import function% d from' t = timer (setup =% i, stmt = 'func% d ()'% i) print 'func% d =% s% s'% (i, T.timeit (10), d [i]) Result
func1 = 16.2272833558 heavy func2 = .00410247671143 attached func3 = .00349569568199 zip Func4 = .00301686387516 izip func5 = .00157338432678% formatting
Comments
Post a Comment