0

Okay, so I am reading a different solution to a problem I had solved. Basically, I have a 2d list of hashes and dots and I want to count hashes in the upper and in the lower part. I don't understand why this program changes the value of 'up' and 'down', in my mind the list named 'fill' should change because it is on the left side of the '='. I understand how the program works, but I don't understand why. Here is the code and an input and output example. (r and s are the numbers of rows and columns, I am interested how the value of 'up' and 'down' changes when it's on the right side (doesn't make sense to me)).

r, s = map(int, input().split())
a=[]
for i in range(r):
    a.append(input())
up, down = [0 for j in range(s)], [0 for j in range(s)]
for j in range(s):
    fill = up
    for i in range(r):
        if a[i][j]=="#":
            fill[j]+=1
        else:
            fill = down
input example:
5 8
########
.###..#.
........
#...#...
########
output example:    
>>>up
[1, 2, 2, 2, 1, 1, 2, 1]
>>> down
[2, 1, 1, 1, 2, 1, 1, 1]
  • `fill = up` here you say fill should point to the same list as up. So when you change fill, up will change since they are both pointing at the same list. same will happen with `fill = down` do you mean to take a copy of these rather than point to the same list? – Chris Doyle Jul 27 '21 at 12:54
  • @YevhenKuzmovych yup , that's what I was wondering. – randommikrovalna Jul 27 '21 at 13:00
  • @ChrisDoyle thanks, I didn't know that those two lists refer to the same list after the assignment. – randommikrovalna Jul 27 '21 at 13:01

0 Answers0