16 November 2010

How to, in Python, iterate through 2D list at once clearly?

I found that this problem is frequently occurred during my python software development. Therefore I did a bit of research and found this link: http://tinyurl.com/2czv57z

So, one neat solution is to use functions in itertools module. Here is how:

from itertools import izip
  for a_row,b_row in izip(alist, blist):
    for a_item, b_item in izip(a_row,b_row):
      if a_item.isWhatever:
        b_item.doSomething()

No comments: