python - Iterating through array -


i have array of bools , want swap entries numbers.

false => 0 true => 1 

i have written 2 different pieces of code , know, 1 better , why. not solving problem, learning.

arr = [[true,false],[false,true],[true,true]]  i,row in enumerate(arr):     j,entry in enumerate(row):         if entry:             arr[i][j] = 1         else:             arr[i][j] = 0 print(arr) 

and second approach:

arr = [[true,false],[false,true],[true,true]]  in range(len(arr)):     j in range(len(arr[i])):         if arr[i][j]:             arr[i][j] = 1         else:             arr[i][j] = 0     print(arr) 

i read there ways importing itertools or similar. not fan of importing things if can done “on-board tools”, should rather using them problem?

let's define array:

>>> arr = [[true,false],[false,true],[true,true]] 

now, let's convert booleans integer:

>>> [[int(i) in row] row in arr] [[1, 0], [0, 1], [1, 1]] 

alternatively, if want more flexible gets substituted in, can use ternary statement:

>>> [[1 if else 0 in row] row in arr] [[1, 0], [0, 1], [1, 1]] 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -