-2

Let us say a txt file consists of these lines:

A
  1
  2
  3
B 
  1
  2
  3
C
  1
  3

Clearly, 2 is missing from C in the txt file. What is the idea to detect the missing 2 and output it? I need to read the txt file line by line.

Thank you !

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Poker Prof
  • 169
  • 5
  • 15
  • 1
    filehandle.`readline` function can help you – avasal Jul 16 '12 at 06:57
  • 4
    Statements like "Clearly, 2 is missing from C" make much more sense to humans than to the Python interpreter. What do the lines represent, and why is it "clear" that a value is missing from a particular section? – Marius Jul 16 '12 at 07:01
  • Ok I have tried to append values 1, 2 and 3 into a list. The list will be reset whenever the line reads a new alphabet such as A,B or C. My idea was if currently in alphabet C the len of the list appended is only 2, then there is a digit missing from C. – Poker Prof Jul 16 '12 at 07:08
  • Basically I want to detect the missing digit from each alphabet. If there are 5 digits from 1 to 5 and say alphabet D only has 1,2,3,4. Then digit 5 is missing from D. I need the idea to do this. Hmmm... – Poker Prof Jul 16 '12 at 07:10
  • @Marius The text file is given as such. The digit 2 is missing in section C. – Poker Prof Jul 16 '12 at 07:14
  • Your file seems to be structured by indentation. Is that the criterion to use to decide whether a line represents a "heading" or part of the content? – Tim Pietzcker Jul 16 '12 at 07:14
  • do you know the digits your suppose to have in advance? – xvatar Jul 16 '12 at 07:15

2 Answers2

0

Probably you want something like this:

line_sets = []
file_names = ['a', 'b', 'c']

# read content of files, if you need to remember file names, use
# dict instead of list
for file_name in file_names:
    with open(file_name, 'rb') as f:
        line_sets.append(set(f.readlines()))

# find missing lines
missing_lines = set()
for first_set, second_set in itertools.combinations(line_sets, 2):
    missing_lines.add(first_set - second_set)
print 'Missing lines: %s' % missing_lines
simplylizz
  • 1,686
  • 14
  • 28
-1

Ok I think the question is not clear to most of you. Anyway here is my solution:

I append the values from each section into a list inside a for loop. For example, in section A, the list will contains 1,2 and 3. The len of the list in section C will be only 2. Thus, we know that a value is missing from section C. From here, we can print out section C. Sorry for the misunderstandings. This question is officially close. Thanks for the view anyway!

Poker Prof
  • 169
  • 5
  • 15