How can I compute elements from two different lists in python? -


i have 2 lists of ints, , want systematically operate objects. example have:

a = [ a1, a2, a3, a4 ...] b = [ b1, b2, b3 ...] 

and want print this:

a1+b1   a2 a2+b2   a3 a3+b3   a4 

i think there "for loop" way, don't know how use 2 variables in "for loop".

you use zip:

>>> = ['a1', 'a2', 'a3', 'a4'] >>> b = ['b1', 'b2', 'b3'] >>> zip(a[:3], b, a[1:]) [('a1', 'b1', 'a2'), ('a2', 'b2', 'a3'), ('a3', 'b3', 'a4')] >>> a, b, c in zip(a[:3], b, a[1:]): ...     print + '+' + b + '   ' + c ...  a1+b1   a2 a2+b2   a3 a3+b3   a4 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -