I have two lists which I use the following function to assign line numbers (similar to nl in unix):
def nl(inFile):
numberedLines = []
for line in fileinput.input(inFile):
numberedLines.append(str(fileinput.lineno()) + ': ' + line)
numberWidth = int(log10(fileinput.lineno())) + 1
for i, line in enumerate(numberedLines):
num, rest = line.split(':',1)
fnum = str(num).rjust(numberWidth)
numberedLines[i] = ':'.join([fnum, rest])
return ''.join(numberedLines)
This retuns lists like: 1: 12 14
2: 20 49
3: 21 28
. With the infile
I am using, the line numbers are very important. My second list is structured the same way but the line numbers mean nothing. I need to find the list differences from the second file and return the line number from the first. So for example: if the second file has: 5: 12 14
48: 20 49
I want to ONLY return 3
which is the line number of missing values from the first list.
Here is what I've tried:
oldtxt = 'master_list.txt' # Line numbers are significant
newFile = 'list2compare.txt' # Line numbers don't matter
s = set(nl(oldtxt))
diff = [x for x in (newFile) if x not in s]
print diff
returns: [12 14\n', '20 49\n', '21 28\n']
-- Clearly not what I need. Any ideas?