Python multidimensional list random changing entries

Built 5x5 list of zeroes and randomly changes exactly ten of those zeroes to ones.
L = [[0 for i in range(5)] for j in range(5)]
c = 0
p = 0
for i in range(len(L)):
    for j in range(len(L)):
        print(L[i][j], end=' ')
    print()
print()
for i in range(len(L)):
    for j in range(len(L)):
        while c < 10:
            i = randint(0, len(L) - 1)
            j = randint(0, len(L)-1)
            if L[i][j] == 0:
                L[i][j] = 1
                c = c + 1
for i in range(len(L)):
    for j in range(len(L)):
        print(L[i][j], end=' ')
    print()


0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

1 1 0 0 0 
0 0 1 0 1 
0 0 1 1 0 
1 0 0 0 0
1 1 0 0 1 

Коментари

Популарни постови са овог блога

Python Fan functions

Python StopWatch milliseconds

Python Accounting