2011/6/25 naheed arafat <naheed...@gmail.com>: > 1) >>>> zip('How are you?'.split(' ')[::-1],'i am fine.'.split(' ')) > [('you?', 'i'), ('are', 'am'), ('How', 'fine.')] >>>> map(lambda i,j:(i,j),'How are you?'.split(' ')[::-1],'i am >>>> fine.'.split(' ')) > [('you?', 'i'), ('are', 'am'), ('How', 'fine.')] > > Which one has better efficiency?
Use timeit. $ python -m timeit -s "q = 'How are you?'.split(' ')[::-1]; a = 'i am fine.'.split(' ')" "zip(q, a)" 1000000 loops, best of 3: 0.558 usec per loop $ python -m timeit -s "q = 'How are you?'.split(' ')[::-1]; a = 'i am fine.'.split(' '); func = lambda i,j:(i,j)" "map(func, q, a)" 1000000 loops, best of 3: 1.24 usec per loop So apparently the first one is twice as fast. Note that function calls in Python are expensive. It's very likely that the lambda call kills the performance here. > 2) > Is there any way easier to do the following? > input: > 'How are you' > 'I am fine' > output: > 'you I are am How fine' > > solution: >>>> ' '.join(reduce(lambda x,y:x+y, zip('How are you'.split(' ')[::-1], > 'I am fine'.split(' ')))) Sorry, I'm too lazy right now to look at it. :) -- Alex | twitter.com/alexconrad _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor