I have an array of bools and now I want to swap those entries for numbers.
False => 0
True => 1
I have written two different pieces of code and I would like to know, which one is better and why. This is not so much about actually solving the problem, as about learning.
arr = [[True,False],[False,True],[True,True]]
for i,row in enumerate(arr):
for j,entry in enumerate(row):
if entry:
arr[i][j] = 1
else:
arr[i][j] = 0
print(arr)
And the second approach:
arr = [[True,False],[False,True],[True,True]]
for i in range(len(arr)):
for j in range(len(arr[i])):
if arr[i][j]:
arr[i][j] = 1
else:
arr[i][j] = 0
print(arr)
I read that there are ways to do this with importing itertools or similar. I am really not a fan of importing things if it can be done with “on-board tools”, but should I rather be using them for this problem?
range(len(...))isn't pythonic.