0

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!

2
  • -54 is actually going to be the maximum value here. Commented Dec 1, 2013 at 21:52
  • Please provide example code of what you have tried. Commented Dec 1, 2013 at 22:05

1 Answer 1

1

Use 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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Ashwini Chaudhary! It works like u said, 1 more thing: How to do after 20 values overwrite the original values like a fifo system ?
@user3055369 Use collections.deque with maxlen=20, to get a FIFO system.
I solved this by using append and pop: lis.append([TrueID,rssi]) d2counter += 1 if d2counter >= 20: lis.pop(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.