*sorted* usage

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

*sorted* usage

Post by blakebr »

Hello,

How is sorted() used when a sort is needed on field other than 0?

This is part of my code.py.

Code: Select all

print("Seq\tECN\tSSID\t\t  dBm\tMAC Address\t\tChan\tFoffs\tFcal")
X = 1
for ap in sorted(esp.scan_APs()):
#    aps.append(ap)
    S = "." * (18 - len(str(ap[1])))
    print("{0:2}\t{1:}\t{2:}{3:}{4:}\t{5:}\t{6:}\t{7:}\t{8:}".format(X,ap[0],ap[1],S,ap[2],ap[3],ap[4],ap[5],ap[6]))
    X += 1
print("  ECN = 0:OPEN  1:WEP  2:WPA_PSK  3:WPA2_PSK  4:WPA_WPA2_PSK  5:WPA2_Enterprise")
This is the result.

Code: Select all

Scanning for WiFi Access Points
Seq     ECN     SSID              dBm   MAC Address             Chan    Foffs  Fcal
 1      0       ESP_F66B11........-47   84:f7:03:f6:6b:11       11      -22    0
 2      0       MOTOROLA-33113....-84   f8:35:dd:ef:f6:b8       1       -19    0
 3      3       ATTBYax2BA........-51   ea:07:b6:e9:f9:68       10      -6     0
 4      3       Blakes 2.4 GHz....-50   d8:07:b6:e9:f9:68       10      -6     0
 5      3       Fios-Q7ZSV........-58   18:78:d4:3c:91:62       1       16     0
 6      3       bblake............-80   10:da:43:cf:3b:de       1       3      0
 7      4       ATTBYax2BA........-75   12:da:43:cf:3b:df       1       3      0
 8      4       Ubiquiti..........-56   c8:3a:35:02:73:e0       10      0      0
 9      4       adafruit..........-44   c0:56:27:c2:40:95       10      -9     0
10      4       adafruit..........-36   e0:b2:60:5c:23:b9       11      -2     0
11      4       bblake............-50   e0:b2:60:65:d2:79       4       -2     0
12      4       bblake............-45   58:d9:d5:59:1f:41       7       -17    0
  ECN = 0:OPEN  1:WEP  2:WPA_PSK  3:WPA2_PSK  4:WPA_WPA2_PSK  5:WPA2_Enterprise
######## ########
Field #0 is ECN, Field #1 is SSID, Field #2 is dBm, Field #3 is MAC Address, Field #4 is Chan, Field #5 is Foffs, Field #6 is Fcal
This is what has been sorted before it is printed.

Code: Select all

[[0, 'ESP_F66B11', -43, '84:f7:03:f6:6b:11', 11, -24, 0], 
[3, 'ATTBYax2BA', -44, 'ea:07:b6:e9:f9:68', 10, -7, 0], 
[3, 'Blakes 2.4 GHz', -47, 'd8:07:b6:e9:f9:68', 10, -7, 0], 
[3, 'Fios-Q7ZSV', -57, '18:78:d4:3c:91:62', 1, 15, 0], 
[3, 'bblake', -71, '10:da:43:cf:3b:de', 1, 3, 0], 
[4, 'ATTBYax2BA', -71, '12:da:43:cf:3b:df', 1, 1, 0], 
[4, 'Ubiquiti', -53, 'c8:3a:35:02:73:e0', 10, -2, 0], 
[4, 'adafruit', -42, 'c0:56:27:c2:40:95', 10, -9, 0], 
[4, 'adafruit', -34, 'e0:b2:60:5c:23:b9', 11, -2, 0], 
[4, 'aristotle', -61, '50:0f:f5:6b:74:01', 6, 0, 0], 
[4, 'aristotle', -52, 'e0:b2:60:52:f9:99', 3, 13, 0], 
[4, 'bblake', -48, 'e0:b2:60:65:d2:79', 4, -2, 0], 
[4, 'bblake', -41, '58:d9:d5:59:1f:41', 7, -17, 0]]

I want to sort on the dBm field. I have searched on sorted and have not found documentation or a tutorial.

Thanks,
Bruce

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: *sorted* usage

Post by dastels »

Pass in an optional key function to extract the value to compare. E.g. given your array of data:

Code: Select all

>>> sorted(a, key=lambda x: x[2])
[[3, 'bblake', -71, '10:da:43:cf:3b:de', 1, 3, 0], 
 [4, 'ATTBYax2BA', -71, '12:da:43:cf:3b:df', 1, 1, 0],
 [4, 'aristotle', -61, '50:0f:f5:6b:74:01', 6, 0, 0], 
 [3, 'Fios-Q7ZSV', -57, '18:78:d4:3c:91:62', 1, 15, 0], 
 [4, 'Ubiquiti', -53, 'c8:3a:35:02:73:e0', 10, -2, 0], 
 [4, 'aristotle', -52, 'e0:b2:60:52:f9:99', 3, 13, 0], 
 [4, 'bblake', -48, 'e0:b2:60:65:d2:79', 4, -2, 0], 
 [3, 'Blakes 2.4 GHz', -47, 'd8:07:b6:e9:f9:68', 10, -7, 0], 
 [3, 'ATTBYax2BA', -44, 'ea:07:b6:e9:f9:68', 10, -7, 0], 
 [0, 'ESP_F66B11', -43, '84:f7:03:f6:6b:11', 11, -24, 0], 
 [4, 'adafruit', -42, 'c0:56:27:c2:40:95', 10, -9, 0], 
 [4, 'bblake', -41, '58:d9:d5:59:1f:41', 7, -17, 0], 
 [4, 'adafruit', -34, 'e0:b2:60:5c:23:b9', 11, -2, 0]]
Dave

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: *sorted* usage

Post by blakebr »

Thank you.

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: *sorted* usage

Post by blakebr »

Where did you find the documentation? I would like to see what other options are available.
Thank you.

Bruce

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: *sorted* usage

Post by dastels »

I just googles "python sorted". There are several sources of documentation. I just checked the first one.

That said, https://docs.python.org/3/howto/sorting.html is very comprehensive. I generally tend to look for things at https://docs.python.org/3/.

Dave

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: *sorted* usage

Post by blakebr »

I was googling circuitpython sorted. Duh

User avatar
dastels
 
Posts: 15662
Joined: Tue Oct 20, 2015 3:22 pm

Re: *sorted* usage

Post by dastels »

The caveat is that not everything in standard Python is available in CircuitPython, so you're not wrong. It's just a lot easier to find thing for "python". Always play with it in the REPL to verify that it works as expected.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”