Python remove all lines which have common value in fields
Question: I have lines of data comprising of 4 fields

    
    
    aaaa bbb1 cccc dddd  
    aaaa bbb2 cccc dddd  
    aaaa bbb3 cccc eeee  
    aaaa bbb4 cccc ffff  
    aaaa bbb5 cccc gggg  
    aaaa bbb6 cccc dddd
    

Please bear with me.

The first and third field is always the same - but I don't need them, the 4th
field can be the same or different. The thing is, I only want 2nd and 4th
fields from lines which don't share the common field. For example like this
from the above data

    
    
    bbb3 eeee  
    bbb4 ffff    
    bbb5 gggg
    

Now I don't mean deduplication as that would leave one of the entries in. If
the 4th field shares a value with another line, I don't want any line which
ever had that value.

humblest apologies once again for asking what is probably simple.


Answer: Here you go:

    
    
    from collections import defaultdict
    
    LINES = """\
    aaaa bbb1 cccc dddd
    aaaa bbb2 cccc dddd
    aaaa bbb3 cccc eeee
    aaaa bbb4 cccc ffff
    aaaa bbb5 cccc gggg
    aaaa bbb6 cccc dddd""".split('\n')
    
    # Count how many lines each unique value of the fourth field appears in.
    d_counts = defaultdict(int)
    for line in LINES:
        a, b, c, d = line.split()
        d_counts[d] += 1
    
    # Print only those lines with a unique value for the fourth field.
    for line in LINES:
        a, b, c, d = line.split()
        if d_counts[d] == 1:
            print b, d
    
    # Prints
    # bbb3 eeee
    # bbb4 ffff
    # bbb5 gggg
    

