1

im trying to solve a problem, But while im debugging , I found that the value of array won't change after once. Is there something i did wrong in this short 5 line of code?? thanks for the answer in advance.

a = [1, 2, 3, 4, 5]

for i in range(5):  
    b, c = map(int, input().split())
    a[a.index(b)], a[a.index(c)] = a[a.index(c)], a[a.index(b)]

print(a)

example input:

5 4
1 2
3 4
1 4
2 2

expected output:

3 1 4 2 5

my output:

1 2 3 5 4
5
  • 2
    Instead of using input(), can you hardcode a known input that fails? Commented Mar 15, 2020 at 0:37
  • sorry, I just edited it ! Commented Mar 15, 2020 at 0:41
  • Please give us a minimal, reproducible example; don't expect every one of us to type input for you. Commented Mar 15, 2020 at 0:42
  • What is this program supposed to do? Why do you expect other output? It obviously works the way you programmed it -- computers are rather annoying in that respect. Commented Mar 15, 2020 at 0:43
  • @harveychoi this is actually an interesting question, but you've made it hard to understand. You can show a minimal example with just: a[a.index(1)], a[a.index(2)] = a[a.index(2)], a[a.index(1)] — you don't need all the input() business — and then simply ask why it is not swapping elements as you might expect. Commented Mar 15, 2020 at 0:48

1 Answer 1

2

you can use:

for i in range(5):  
    b, c = map(int, input().split())
    b_index = a.index(b)
    c_index = a.index(c)
    a[b_index], a[c_index] = a[c_index], a[b_index]

here is a piece of code to understand the behavior:

a = [1, 2, 3, 4, 5]

def get_index(i, s):
    idx = a.index(i)
    print(s, i, idx)
    return idx

a[get_index(1, 'left')], a[get_index(2, 'left')] = a[get_index(2, 'right')], a[get_index(1, 'right')]
print(a)

output:

right 2 1
[1, 2, 3, 4, 5]
right 1 0
[1, 2, 3, 4, 5]
left 1 0
[1, 2, 3, 4, 5]
left 2 0
[2, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

as you can see get_index(2, 'left') is not the same with get_index(2, 'right'), this shows that a.index(c) doesn't have the same value in the left and in the right side of the = operator


back to your code:

a[a.index(b)], a[a.index(c)] = a[a.index(c)], a[a.index(b)]

first, it is executed the code from the right side of = operator so you have (c, b), then a[a.index(b)] will take the value c, at this step you have 2 of c in your list a, when a.index(c) it is evaluated will return the first c found from the left to right, in the final step you want to set value b to where c was in the beginning, but now you have 2 of c, if c < b you will end with the desired output because the method list.index will return the position of actual c; if b < c then b will take the place where b was in the beginning and this will result in no change in your list a


example:

case b < c
a = [1, 2, 3, 4, 5] --> swap 2 with 5; b = 2, c = 5
a[a.index(b)], a[a.index(c)] =  5, 2
a[a.index(b)] = 5 --> a = [1, 5, 3, 4, 5]
      -->  a.index(5) --> --> ↑
      ↑
a[a.index(c)] = 2 --> a =  [1, 2, 3, 4, 5]


case b > c
a = [1, 2, 3, 4, 5] --> swap 2 with 5; b = 5, c = 2
a[a.index(b)], a[a.index(c)] =  2, 5
a[a.index(b)] = 2 --> a = [1, 2, 3, 4, 2]
      -->  a.index(2) --> --> ↑
      ↑
a[a.index(c)] = 5 --> a =  [1, 5, 3, 4, 2]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.