Merging contents of two lists based on a if-loop

Posted by chavanak on Stack Overflow See other posts from Stack Overflow or by chavanak
Published on 2010-03-24T11:06:24Z Indexed on 2010/03/24 11:13 UTC
Read the original article Hit count: 213

Filed under:
|
|

I have a minor problem while checking for elements in a list: I have two files with contents something like this

file 1:        file2:
 47            358 47
 48            450 49
 49            56 50

I parsed both files into two lists and used the following code to check

for i in file_1:
   for j in file_2:
      j = j.split()
      if i == j[1]:
        x=' '.join(j)

I am now trying to get a "0" if the value of file_1 is not there in file_2 for example, value "48" is not there is file_2 so I need to get the output like (with only one space in between the two numbers):

output_file:
  358 47
   0 48
  450 49
   56 50

I tried using the dictionary approach but I didn't quite get what I wanted (actually I don't know how to use dictionary in python correctly ;)). Any help will be great.

© Stack Overflow or respective owner

Related posts about python

Related posts about loops