Collapse information according to certain column of a line
Question: For the matrix as below

    
    
        A  20  200
        A  10  150
        B  60  200
        B  80  300
        C  90  400
        C  30  300
    

My purpose is trying to: for each category (labelled as A,B,C..in the 1st
column), I'd like to find the minimum as well as maximum numbers (as biggest
range). So expect to see:

    
    
    A  10  200
    B  60  300
    C  30  400
    

So how could I do using Python?


Answer: I would start by:

    
    
    maxs, mins = {}, {}
    for line in fd:
        category, small, big = line.split()
        if category not in maxs or big > maxs[category]:
            maxs[category] = big
        if category not in mins or small < mins[category]:
            mins[category] = small
    
    # final printings
    for category in maxs:
        print(category, mins[category], maxs[category], sep='\t')
    

This returns dicts, that can be merged using `{c: (mins[c], maxs[c]) for c in
maxs}`.

This code assume that an iterable of lines is named `fd`. Could be an opened
file containing the matrix in raw text.

If the order is important, a good solution is to use an
[OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict)
instead of the regular dict for `mins` and `maxs`.

