sovel2
sovel2

Reputation: 415

changing 2d list values using loop works uncorrectly

brief = B[:8, :8]

average = 0
for i in range(8):
    average += sum(brief[i])
average -= brief[0][0]
average /= 64

print(average, 'is average\n\n')


for i in range(0, 8):
    for j in range(0, 8):
        if brief[i][j] >= average:
            brief[i][j] = 0
            print(brief[i][j])
        if brief[i][j] < average:
            brief[i][j] = 1
            print(brief[i][j])

for b in range(8):
    for bb in range(8):
        print(brief[b][bb])

brief is a 8x8 part of B matrix with these values:

[[-79.59774442081138,    158.7202095869316,    112.56820969421881,    -59.04458113044859,    110.79507835753822,    65.46782110183209,    -1.3342162705874172,    25.437988304756363],
[322.5377343539482,     8659.5625,             456.1372383004441,     1039.2670348171582,    502.43880254201326,    201.0009891626345,    209.16093093311468,     -208.50047755598916],
[-112.56820969421881,   224.46427302053877,   -159.19548884162273,   -83.50164741931893,    156.6879024574203,     92.58548050122643,    -1.8868667450035765,    35.974748060074624],
[37.72684830839333,     624.7408144869114,    53.3538205433223,      -449.0466120624775,    32.59027278985499,     -43.22267150470408,   -85.65936443956468,     219.2142421380014],
[-28.001674523528344,   249.9757286483847,    -39.60034788033096,    31.11673361617372,     130.48068801733655,    -160.1201473099224,   -112.78964698763158,    -158.7758135283589],
[54.7357856623738,      -359.607876182155,    77.40809043087583,     46.68603856944288,     -89.88043774029418,    282.9769031346061,    27.867634849967388,     -202.2926232693297],
[15.271034089670682,    70.44041778968491,    21.596503521074148,    7.9278092841192915,    -37.515872842738105,   27.38507374399468,    -10.177891707673886,    -118.39173788971809],
[20.304659220046318,    -234.8242040396121,   28.71512444835341,     146.63941111295003,    -59.20821857294881,    -73.98127851564756,   121.3747483435147,      17.835871018082692]]

Then I find average, and change every element of brief to 0 if it's greater (or equal) than average, and to 1 if it's less than average. In the loop where I compare brief elements to average I also print the new values. So the output is 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 etc.... As you can see there are 0s and 1s. BUT! When I print brief after that loop I get all elements as 1s. What's wrong with the output?

P.S. B was created as np.zeros then filled with numbers

Upvotes: 0

Views: 29

Answers (1)

Sheldore
Sheldore

Reputation: 39052

Replace your double if statements with the following lines:

for i in range(0, 8):
    for j in range(0, 8):
        if brief[i][j] >= average:
            brief[i][j] = 0
        else:
            brief[i][j] = 1

The problem was that in your code you first assign brief[i][j] = 0 and then you again check in the second if statement if brief[i][j] < average: which means if 0 < average, which is always true for your value of average and hence everything becomes 1

Upvotes: 2

Related Questions