-1

I'm trying to rearrange the row by ordering.

if "MCS7" changes to "MCS11" as below list, How will you code?

list = [['', '', '1', '2412', 'MCS11', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS5', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Burst Power (Average)', '-100', '30', '12.02', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM All Carriers (Average)', '', '-27', '-32.08', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Data Carriers (Average)', '', '-27', '-31.98', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Pilot Carriers (Average)', '', '-8', '-33.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Center Freq. Error (Average)', '-60300', '60300', '763.52', 'Hz', 'PASS']]
for l in sorted(list, key = lambda x: ( x[4], x[5] ) ):
    print l

I expect the output that as below, by ordering "MCS5~MCS11".

['', '', '1', '2412', 'MCS5', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS']]
['', '', '1', '2412', 'MCS5', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Burst Power (Average)', '-100', '30', '12.02', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM All Carriers (Average)', '', '-27', '-32.08', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Data Carriers (Average)', '', '-27', '-31.98', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Pilot Carriers (Average)', '', '-8', '-33.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Center Freq. Error (Average)', '-60300', '60300', '763.52', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS11', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS11', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
Alvin
  • 1
  • 2
  • Possible duplicate of [How to sort (list/tuple) of lists/tuples?](https://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples) – Patrick Artner May 10 '19 at 08:33

1 Answers1

1

You would need to sort your list based on one element of your inner lists. A similar questions (using list tuples are is asked here: How to sort (list/tuple) of lists/tuples? )

You can use the key paramter of sort()/sorted() and a lambda or 8operator.itemgetter](https://docs.python.org/2/library/operator.html#operator.itemgetter) for it :

data = [
['', '', '1', '2412', 'MCS7', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS7', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS7', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS7', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS7', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS5', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS5', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Burst Power (Average)', '-100', '30', '12.02', 'dBm', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM All Carriers (Average)', '', '-27', '-32.08', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Data Carriers (Average)', '', '-27', '-31.98', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'EVM Pilot Carriers (Average)', '', '-8', '-33.56', 'dB', 'PASS'],
['', '', '1', '2412', 'MCS6', 'Center Freq. Error (Average)', '-60300', '60300', '763.52', 'Hz', 'PASS']]

# sort by 5th value and 6th value so that same 5th are alphabetically sorted
for l in sorted(data, key = lambda x: ( x[4], x[5] ) ):
    print l

Output:

['', '', '1', '2412', 'MCS5', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS']
['', '', '1', '2412', 'MCS5', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS']
['', '', '1', '2412', 'MCS5', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS']
['', '', '1', '2412', 'MCS5', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS']
['', '', '1', '2412', 'MCS5', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS']
['', '', '1', '2412', 'MCS6', 'Burst Power (Average)', '-100', '30', '12.02', 'dBm', 'PASS']
['', '', '1', '2412', 'MCS6', 'Center Freq. Error (Average)', '-60300', '60300', '763.52', 'Hz', 'PASS']
['', '', '1', '2412', 'MCS6', 'EVM All Carriers (Average)', '', '-27', '-32.08', 'dB', 'PASS']
['', '', '1', '2412', 'MCS6', 'EVM Data Carriers (Average)', '', '-27', '-31.98', 'dB', 'PASS']
['', '', '1', '2412', 'MCS6', 'EVM Pilot Carriers (Average)', '', '-8', '-33.56', 'dB', 'PASS']
['', '', '1', '2412', 'MCS7', 'Burst Power (Average)', '-100', '30', '12.09', 'dBm', 'PASS']
['', '', '1', '2412', 'MCS7', 'Center Freq. Error (Average)', '-60300', '60300', '663.96', 'Hz', 'PASS']
['', '', '1', '2412', 'MCS7', 'EVM All Carriers (Average)', '', '-27', '-31.56', 'dB', 'PASS']
['', '', '1', '2412', 'MCS7', 'EVM Data Carriers (Average)', '', '-27', '-31.45', 'dB', 'PASS']
['', '', '1', '2412', 'MCS7', 'EVM Pilot Carriers (Average)', '', '-8', '-33.38', 'dB', 'PASS']

Read more about sorting:

About lambda: Why are Python lambdas useful?


To sort by the "pure" numerical value, use:

for l in sorted(data, key = lambda x: ( int(x[4][3:]) , x[5]) ):
    print l

The part

int(x[4][3:])

strips away "MCS" by slicing and converts the remainder to an integer as first part of the tuple.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Dear Patrick, if "MCS7" changes to "MCS11", there are "MCS5", "MCS6" and "MCS11". How will you change the coding? Because actual re-arrangement will "MCS11", "MCS5", "MCS6". – Alvin May 14 '19 at 16:04
  • A new issue if "MCS7" changes to "11Mbps", "MCS6" changes to "5.5Mbps", and "MCS6" changes to "1Mbps", how will you code? – Alvin May 14 '19 at 16:54
  • How would you? I am not providing answers to every follow up question you got. In software development processes this is called [feature or scope creep](https://stackoverflow.com/questions/447344/). You do the same as for integers: you find a way to split the data you want how you want it and then make it a number (int/float) - you can write a small helper function for that and apply it `(helper(x[4]),x[5])` instead of `( int(x[4][3:]) , x[5])`. In your case your helper could remove all Letters from the string - unless you also got `'34Evil14Things2Do'` => `34142` – Patrick Artner May 14 '19 at 16:57
  • Sorry for further specific issue that makes you unhappy since I thought you may have clever way to sort "11Mbps,5.5Mbps,1Mbps". As your final words, I've tried remove all letters from string. Thanks. – Alvin May 15 '19 at 01:17
  • Dear Patrick, On "11Mbps,5.5Mbps,1Mbps" condition, I try the code without remove string just for your reference as below, and thanks for your suggestion key function, "lambda". for l in sorted(data, key = lambda x: ( int(x[3]) , eval(x[4].split('M')[0]))): print l – Alvin May 15 '19 at 03:35