Averaging column 2D array python
Question: I have a 2D arraylist data which fills with a loop like this:

    
    
    data.append([TrueID,rssi])
    

after 8 times i got this value for data:

    
    
    data =
    
        [['469420270013002A', -90], 
         ['469420270005000C', -89], 
         ['469420270013002A', -94], 
         ['4694202700270003', -53], 
         ['469420270005000C', -91], 
         ['469420270013002A', -92], 
         ['4694202700270003', -55]]
    

I want to calculate the average RSSI value of each TrueID and return the
lowest RSSI value with its TrueID.

So I need output:

    
    
    print "The weakest ID is " ID_result " with Rssi value of " rssi_result
    >>The weakest ID is '4694202700270003' with Rssi value of -54
    

It's necessary the 2D array fills until 20 values and continues like a FIFO
system.

All suggestions are welcome (even if you know other methods to get to the same
result)!

Thank you!


Answer: Use
[`collections.defaultdict`](http://docs.python.org/2/library/collections.html#collections.defaultdict)
and `max`:

    
    
    >>> from collections import defaultdict                                   
    >>> lis = [['469420270013002A', -90],                                 
         ['469420270005000C', -89], 
         ['469420270013002A', -94], 
         ['4694202700270003', -53], 
         ['469420270005000C', -91], 
         ['469420270013002A', -92], 
         ['4694202700270003', -55]]
    >>> d = defaultdict(list)                
    >>> for k, v in lis:                                                      
        d[k].append(v)
    ...     
    

Now `d` contains:

    
    
    >>> d
    defaultdict(<type 'list'>,
    {'469420270005000C': [-89, -91],
     '4694202700270003': [-53, -55],
     '469420270013002A': [-90, -94, -92]})
    

Now use `max` and a dict comprehension to calculate the average and find out
the max (key, value) pair:

    
    
    >>> max({k:sum(v)/float(len(v)) for k, v in d.items()}.items(), key=lambda x:x[1])
    ('4694202700270003', -54.0)
    

