Say I have a sorted list of strings as in:
[\'A\', \'B\' , \'B1\', \'B11\', \'B2\', \'B21\', \'B22\', \'C\', \'C1\', \'C11\', \'C2\']
Now I wan
import numpy as np
def sort_with_prefix(list, prefix):
alist = np.array(list)
ix = np.where([l.startswith(prefix) for l in list])
alist[ix] = [prefix + str(n or '')
for n in np.sort([int(l.split(prefix)[-1] or 0)
for l in alist[ix]])]
return alist.tolist()
For example:
l = ['A', 'B', 'B1', 'B2', 'B11', 'B21', 'B22', 'C', 'C1', 'C2', 'C11']
print(sort_with_prefix(l, 'B'))
>> ['A', 'B', 'B1', 'B2', 'B11', 'B21', 'B22', 'C', 'C1', 'C11', 'C2']