-1

The below code is changing the value of the list K automatically, please help me know why

1
4
1 2 4 3

I gave the above input

The output is this

[1, 2, 3, 4]
[1, 2, 3, 4] 1
[1, 2, 3, 4] 2
1 1
[1, 2, 3, 4] 2
2 2
[1, 2, 3, 4] 2
3 4
[1, 2, 3, 4] 2
4 3
[1, 2, 4, 3]
[1, 2, 4, 3] 1
[1, 2, 4, 3] 2
1 1
[1, 2, 4, 3] 2
2 2
[1, 2, 4, 3] 2
4 4
[1, 2, 4, 4] 2
4 3
[1, 2, 4, 4]
[1, 2, 4, 4]

In the last steps the value of list k changed automatically.

This was done on PyPy 3 and python 3.7

t=int(input())
while t!=0:
    t-=1
    n=int(input())
    h=[0]*n
    lt=[0]*n
    c=0
    l=[int(i) for i in input().split()]
    k=[i for i in range(1,n+1)]
    print(k)
    while c!=7:
        print(k, end=" ")
        print("1")
        c+=1
        for i in range(n):
            print(k, end=" ")
            print("2")
            lt[l[i]-1]=k[i]
            print(k[i],end =" ")
            print(l[i])
        k=lt
        print(k)
        for i in range(n):
            if k[i]==l[i] and h[k[i]-1]==0:
                h[k[i]-1]=c
    print(*h,sep=" ")
Manish
  • 3
  • 2
  • Please dont mind the end output just the list k is not proper – Manish Oct 22 '19 at 16:06
  • `k = lt` makes `k` refer to the *same* list object as `lt`; it does not make `k` refer to a copy of the current value of `lt`. – chepner Oct 22 '19 at 16:06
  • 1
    Please try to create a _minimal_ example which has the problem, so that we don't have to go through all of that. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – zvone Oct 22 '19 at 16:07
  • but then k value is updated only after the loop ended – Manish Oct 22 '19 at 16:08
  • Doesn't matter; the next time you enter that loop, `lt` *also* still points to the same list; you didn't create a new list for `lt` to refer to. – chepner Oct 22 '19 at 16:09
  • Related: it's not at all obvious what this code is *supposed* to do. I'm just point how `k` *can* be modified, by way of modifying the list using `lt`. – chepner Oct 22 '19 at 16:10
  • the output marked with 2 in the end is the loop output, the value of k within the loop shouldn't change because no modification is made but it changes please give it a look – Manish Oct 22 '19 at 16:11
  • Yes, a modification is made to `k`. After `k = lt`, the assignment `lt[l[i] - 1] = k[i]` is equivalent to `k[l[i] - 1] = k[i]`. You might want `k = lt[:]` instead. – chepner Oct 22 '19 at 16:13
  • but sir the modification you are talking of is made only after the loop is ended right? – Manish Oct 22 '19 at 16:16
  • the value of k when it entered the loop should be the same when its exiting too, because it is undisturbed inside the loop – Manish Oct 22 '19 at 16:18
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) applies here. We cannot effectively help you until you post your MCVE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Most of the code you posted is unrelated to the problem at hand. Useless variable names almost ensure that we won't *want* to work on this. – Prune Oct 22 '19 at 16:34

1 Answers1

0

You need to supply a new copy of list to k for the desired functionality.

Everything in Python is an Object. when you do k=lt, you actually give the reference contained in variable lt to k.

you can check the above fact by print(id(k),id(lt)) after the assignment.

There are many methods for creating a new list from an existing list. You can refer to this stackoverflow question for most of them.

For the above question, replace line 19 (k=lt) in your code with either k=lt.copy() or k=list(lt).