Python:Sorting 2d arrays generated in a loop causing errors
<p>This program randomly generates weights for several artificial neural networks using a loop, calculates the output of the network and appends that to the end of each array, then appends the cost to the end of each array. When run without sorting the arrays this program does all calculations correctly, but when I try to sort the array by the last item in each array it seems like it breaks the loop even though the sort function is called after all the other functions.</p>

<pre><code>w, h = 9, 10
network = [[0 for x in range(w)] for y in range(h)]

def sigmoid(sigin):
  return 1 / (1 + math.exp(-sigin))
def netcal(x):
    network[x].append(sigmoid((sigmoid(i1*network[x][0]+i2*network[x][1])*network[x][6])+(sigmoid(i1*network[x][2]+i2*network[x][3])*network[x][7])+(sigmoid(i1*network[x][4]+i2*network[x][5])*network[x][8])))
def seed():
    b = 0
    while b &lt; 10:
        y = 0
        while y &lt; 9:
            network[b][y] = random.random()
            y += 1
        b += 1
def calall():
    c = 0
    while c &lt; 9:
        netcal(c)
        print(network[c][9])
        c += 1
def cost():
    d = 0
    while d &lt; 9:
        network[d].append(1 - network[d][9])
        print(network[d][10])
        d += 1
def sort():
    sorted(network, key=lambda x: x[10])
def wait():
    m.getch()


i1=0
i2=1

seed()
calall()
print("break")
cost()
sort()
print(network)
</code></pre>

<p>Your current approach sorts the list but does not <code>return</code> the result nor update the network. Note that <code>sorted</code> <em>returns</em> a sorted list. </p>

<p>You want an <em>in-place</em> sort:</p>

<pre><code>def sort():
    network.sort(key=lambda x: x[-1]) # or x[8]
#                                ^^
</code></pre>

python neural-network