my question seperates in two parts.
first I need to sort a list like this:
a = ['Zone 3', 'Zone', 'Zone 9', 'Zone 10', 'Zone 5']
by the number of the Zone.
The result should look like this:
result = ['Zone', 'Zone 3', 'Zone 5', 'Zone 9', 'Zone 10']
I have tried it with the following code:
import numpy as np
a = ['Zone 3', 'Zone', 'Zone 9', 'Zone 10', 'Zone 5']
b = np.empty((0,2))
for i in range(0, len(a)):
if len(a[i]) > 4:
a1 = a[i].split()
a1 = np.array([[a1[0],int(a1[1])]])
if len(a[i]) == 4:
a1 = np.array([[a[i], '']])
print(a2)
b = np.append(b, a1, axis=0)
b = b[b[:,1].argsort()]
print(b)
with the result:
[['Zone' '']
['Zone' '10']
['Zone' '3']
['Zone' '5']
['Zone' '9']]
The problem seems to be, that argsort() does not recognize the 10 as 10 but as 1.
The second problem accured in the folling code:
seperator = ' '
b = list()
for i in range(len(a)):
c = seperator.join(b[i])
print(c)
b = np.append(b, c)
print(b)
The error Exception has occurred: IndexError list index out of range appears, but the index should be in range.
I hope you can help me with these problems.