Reputation: 135
I have a set of codes attached with an index:
alpha_numeric_set = {
"ASN 111",
"ASN 125",
"ASP 105",
"ASP 97",
"GLN 130",
"GLN 66",
"GLY 107",
"ILE 100",
"ILE 112",
"ILE 98",
"LEU 129",
"LEU 131",
"LEU 99",
"LYS 127",
"PHE 103",
"PHE 153",
"PRO 110",
"PRO 128",
"PRO 96",
"SER 109",
"THR 90",
"TRP 68",
"TYR 126",
"TYR 64",
"VAL 106",
"VAL 115",
"VAL 132",
"VAL 95 "
}
I want to be able to sort each string by the index number chronologically and the code name alphabetically. I've managed to get half of what I want, (ie, using sorted(alpha_numeric_set)
to get the codes sorted alphabetically ) but I can't figure out how to sort the codes along with the index numbers.
I would like the set to output as
ASP 97
ASP 105
GLN 66
GLN 130
etc, etc
And not as:
ASP 105
ASP 97
GLN 130
GLN 66
etc, etc
I've tried breaking up each string using .split()
and then rejoining each string but that pairs the index with the wrong code.
Upvotes: 0
Views: 48
Reputation: 3194
What you want to achieve here is the natural sort ordering. You can, of course, do this manually for your data, which has a simple pattern, but there's a library named natsort
that implements natural sort for any strings:
from natsort import natsorted
result = natsorted(alpha_numeric_set)
print(result)
# ['ASN 111', 'ASN 125', 'ASP 97', 'ASP 105', 'GLN 66', 'GLN 130' ...
Upvotes: 1
Reputation: 61910
You could do the following:
def alpha_numeric_key(e):
s, t = e.split()
return s, int(t)
result = sorted(alpha_numeric_set, key=alpha_numeric_key)
print(result)
Output
['ASN 111', 'ASN 125', 'ASP 97', 'ASP 105', 'GLN 66', 'GLN 130', 'GLY 107', 'ILE 98', 'ILE 100', 'ILE 112', 'LEU 99', 'LEU 129', 'LEU 131', 'LYS 127', 'PHE 103', 'PHE 153', 'PRO 96', 'PRO 110', 'PRO 128', 'SER 109', 'THR 90', 'TRP 68', 'TYR 64', 'TYR 126', 'VAL 95 ', 'VAL 106', 'VAL 115', 'VAL 132']
Upvotes: 4