Search code examples
pythonfor-loopoperation

Python : Composite operator "+=" works twice in for statement


I have some data as a nested list like the following :
(I'm sorry to say that StackOverFlow denied my original post, so I uploaded it as images.)

enter image description here

The third value of each inner list is an integer value that starts from 0,
but I want to convert it to start from 1 by adding 1.

These are my first codes for the purpose and its results.

codes :

readlocation = (0, 2, 1, 28) + tuple(list(range(7, 13))) + (18,)
# print(readlocation)                                        # (0, 2, 1, 7, 8, 9, 10, 11, 12, 18)

print("이름", "출현연도", "출현지역", "혈연", "출생연도", "지력", "무력", "매력", "의리", "인덕", "야망", "상성")

for i in list(range(0, 10)) :                                # Test
# for i in list(range(0, len(general_offset_init) - 2)) :      # The last two rows are empty

    print(bytes(general_data[i][31:46]).decode('utf-8').ljust(15), " ", end='')                 # name : [31:46]

    for j in readlocation :
        general_data[i][2] += 1                                 # Wrong results but can't find the reason
        print(str(general_data[i][j]).rjust(3), " ", end='')    # Other values

    print(" ")

results :
enter image description here

To my surprise, All the values adding 1 increase by 2(ex. 20 → 22).

But, when I change the codes like the below, it works well(ex) 20 → 21).

codes :

for i in list(range(0, 10)) :                                # Test
# for i in list(range(0, len(general_offset_init) - 2)) :      # The last two rows are empty

    print(bytes(general_data[i][31:46]).decode('utf-8').ljust(15), " ", end='')                 # name : [31:46]

    general_data[i][2] += 1                                                                     # Right ex) Zhao Yue / 3
    print(str(general_data[i][2]).rjust(3))                                        # appearance province

results :
enter image description here

Where is any missing part or my wrong idea? Thank you for helping me.


Solution

  • OMG I found where was wrong. I missed so basic thing, but it was really scaring mystery before I found it. I expect you guys to understand we often used to miss things like this.

    readlocation = (0, 2, 1, 28) + tuple(list(range(7, 13))) + (18,)
    # print(readlocation)                                                                 # (0, 2, 1, 7, 8, 9, 10, 11, 12, 18)
    
    print("이름", "출현연도", "출현지역", "혈연", "출생연도", "지력", "무력", "매력", "의리", "인덕", "야망", "상성")
    
    # for i in list(range(0, 10)) :                                                       # test
    for i in list(range(0, len(general_offset_init) - 2)) :                             # The last two rows are empty
    
        general_data[i][2] += 1                                                         # province# : 0~40 → 1~41
    
        print(bytes(general_data[i][31:46]).decode('utf-8').ljust(15), " ", end='')     # name : [31:46]
        for j in readlocation :                                                         # other values
            print(str(general_data[i][j]).rjust(3), " ", end='')
        print(" ")