我正在尝试通过排序来重新排列行。
如果“ MCS7”更改为“ MCS11”,如下所示, 您将如何编码?
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
我希望通过订购“ 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'],
答案 0 :(得分:1)
您需要根据内部列表中的一个元素对列表进行排序。一种 类似的问题(在此处使用列表元组被询问:How to sort (list/tuple) of lists/tuples?)
您可以使用key
paramter of sort()
/sorted()
和lambda或
8operator.itemgetter](https://docs.python.org/2/library/operator.html#operator.itemgetter):
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
输出:
['', '', '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']
了解有关排序的更多信息:
关于lambda:Why are Python lambdas useful?
要按“纯”数值排序,请使用:
for l in sorted(data, key = lambda x: ( int(x[4][3:]) , x[5]) ):
print l
零件
int(x[4][3:])
通过切片去除“ MCS”,并将其余部分转换为整数,作为元组的第一部分。