python - traversing two lists simultaneously with multiple iterators -


i trying write simple program looks through 2 text files , reports differences between 2 files. function, have 2 lists contain list of words each line (so they're 2d lists). go through , compare each word , report error if not same, storing of errors in list printed user. i iterator report line of error , 2 words reported.

def compare(lines1, lines2):     i, line1, line2 in izip(lines1, lines2):         word1, word2 in izip(line1, line2):             if word1 != word2:                 report_error(i, word1, word2) 

however, not working me. read on stackoverflow need use zip() or izip() function read 2 lists @ once still isn't working me. getting following error.

  file "debugger.py", line 28, in compare     i, line1, line2 in izip(lines1, lines2): valueerror: need more 2 values unpack 

any idea i'm doing wrong? can provide full file if helpful.

zip(), , similar functions, produce tuples of length equal number of passed arguments. for word1, word2 in izip(line1, line2): work, for i, line1, line2 in izip(lines1, lines2): not, you're zipping through 2 iterables, lines1 , lines2, can't unpack two-element tuples 3 references.

to fix this, use enumerate(), adds index. use start=1 start line number of 1 instead of default of 0:

def compare(lines1, lines2):     i, (line1, line2) in enumerate(izip(lines1, lines2), start=1):         word1, word2 in izip(line1, line2):             if word1 != word2:                 report_error(i, word1, word2) 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -