TransWikia.com

Why does this list element move back in the list?

Stack Overflow Asked by user13990775 on January 5, 2022

For context, what I am trying to do is create a top-down display in which the screen is just text which is printed every time the player gives input. What this script does is move the Y upward so that I can find things I could do to move the player when the input is given.

minimap=[
        'HHHHHHHHHHHHHHHHHHHH',
        'H                  H',
        'H                  H',
        'H           HHHHHHHH',
        'H                  H',
        'HHHHHHHH           H',
        'H                  H',
        'H          Y       H',
        'HHHHHHHHHHHHHHHHHHHH']
for li in minimap:
    print(li)
count=0
for b in minimap:
    count+=1
    if count==7:
        soi=b
    elif count==8:
        s2i=b
minimap.pop(7)
minimap.pop(6)
list(soi).pop(11)
list(s2i).pop(11)
list(soi).insert(11,' ')
list(s2i).insert(11,'Y')
minimap.insert(7,soi)
minimap.insert(6,s2i)
for li in minimap:
    print(li)

So what happens in the two lines

minimap.insert(7,soi)
minimap.insert(6,s2i)

Is that the new, changed lines are inserted into the minimap[] positions of the old lines, which were removed from minimap[] (I say this because I believe it is most relevant to the issue).

However this is what happens:

HHHHHHHHHHHHHHHHHHHH
H                  H
H                  H
H           HHHHHHHH
H                  H
HHHHHHHH           H
H                  H
H          Y       H
HHHHHHHHHHHHHHHHHHHH

(New minimap:)

HHHHHHHHHHHHHHHHHHHH
H                  H
H                  H
H           HHHHHHHH
H                  H
HHHHHHHH           H
H          Y       H
HHHHHHHHHHHHHHHHHHHH
H                  H

Everything worked as expected, however, (it seems that) line 8 and (new) line 7 swapped positions. Why did this happen?

One Answer

What happened is that when you

minimap.insert(7,soi)

There were only 6 items left in your minimap, and the 6th item is 'HHHHHHHHHHHHHHHHHHHH'.

So by inserting soi to the 7th item, you are actually putting it after the 'HHHHHHHHH'.

Instead, you would want:

minimap.insert(6,soi)
minimap.insert(6,s2i)

or

minimap.insert(6,s2i)
minimap.insert(7,soi)

Answered by Ranoiaetep on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP