data = ['ID:123 GE:m FN:Amir LN:Maleki MN:0400101010 EM:[email protected] ST:VIC',
'ID:b22 EM:[email protected] ST:VIC GE:F FN:Miniyi LN:Li MN:0430101210 MN:0432101215',
'ID:153 GE:m FN:John LN:Liu MN:040181010 ST:NSW EM:[email protected]']
I want the following data to be arranged in the order of
key_order = 'ID', 'GE', 'FN', 'LN', 'MN', 'EM', 'ST'
I can do it when there is one string using this code:
data = ['ID:153 GE:m FN:John LN:Liu MN:040181010 ST:NSW EM:[email protected]']
data = data[0].split()
keyorder = ['ID', 'GE','FN','LN','MN','EM', 'ST']
print(sorted(data, key=lambda x: key_order.index(x.split(':')[0])))
['ID:153', 'GE:m', 'FN:John', 'LN:Liu', 'MN:040181010', 'EM:[email protected]', 'ST:NSW']
Also having trouble for the final result to be displayed as:
ID:123 GE:m FN:Amir LN:Maleki MN:0400101010 EM:[email protected] ST:VIC
ID:b22 EM:[email protected] ST:VIC GE:F FN:Miniyi LN:Li MN:0430101210 MN:0432101215
ID:153 GE:m FN:John LN:Liu MN:040181010 ST:NSW EM:[email protected]
Thanks heaps