So my prompt is there is a few nested lists (ex.[[0,0,0], [0,0,0]]) and I need to use a tuple ex. [(0,0),(1,2)] to increment the spots in the original lists by 1. It should look like this "[[1,0,0][0,0,1]]" I get i need to probably nest for loops by I'm strugling to figure out the exact details if someone can help that would be great.
What I have so far:
def increment_attendance(seating, locations):
for i in range(len(locs)):
for j in range(len(locs(i)))
seating[i][j][0] = 1
seating[i][j][1] = 1
return seating
CodePudding user response:
Let's think about this problem in words:
for each location
increment the seating at that location by 1
When we do this, we now see that there is only one for loop. So our code should only be a single for loop. There's no need for a nested loop.
The next steps is to clarify what "increment the seating at that location by 1" means. Well, a "location" is an x and y coordinate as a tuple. So now we can change our wording:
for each location
increment the seating by at the (x, y) coordinate of the location
I'll let you translate this into code from here.
CodePudding user response:
You need neither nested for loops nor len
function in the loop. You can do something like:
def increment_attendance(seating, locations):
for innerTuple in locations:
seating[innerTuple[0]][innerTuple[1]] = 1
return(seating)
increment_attendance([[0,0,0], [0,0,0]], [(0,0),(1,2)])
The output of example above would be:
[[1, 0, 0], [0, 0, 1]]
CodePudding user response:
You can unpack tuples in a for loop like so:
def increment_attendance(seating, locations):
for row, column in locations:
seating[row][column] = 1
return seating