Find all the numbers in one file that are not in another file in python

前端 未结 5 2049
灰色年华
灰色年华 2021-01-04 02:08

There are two files, say FileA and FileB and we need to find all the numbers that are in FileA which is not there in FileB. All the numbers in the FileA are sorted and all t

5条回答
  •  伪装坚强ぢ
    2021-01-04 02:32

    As files are sorted you can just iterate through each line at a time, if the line of file A is less than the line of file B then you know that A is not in B so you then increment file A only and then check again. If the line in A is greater than the line in B then you know that B is not in A so you increment file B only. If A and B are equal then you know line is in both so increment both files. while in your original question you stated you were interested in entries which are in A but not B, this answer will extend that and also give entries in B not A. This extends the flexability but still allows you so print just those in A not B.

    def strip_read(file):
        return file.readline().rstrip()
    
    in_a_not_b = []
    in_b_not_a = []
    with open("fileA") as A:
        with open("fileB") as B:
            Aline = strip_read(A)
            Bline = strip_read(B)
            while Aline or Bline:
                if Aline < Bline and Aline:
                    in_a_not_b.append(Aline)
                    Aline = strip_read(A)
                elif Aline > Bline and Bline:
                    in_b_not_a.append(Bline)
                    Bline = strip_read(B)
                else:
                    Aline = strip_read(A)
                    Bline = strip_read(B)
    
    print("in A not in B", in_a_not_b, "\nin B not in A", in_b_not_a)
    

    OUTPUT for my sample Files

    in A not in B ['2', '5', '7'] 
    in B not in A ['6']
    

提交回复
热议问题