Python :复合运算符“+=”在 for 语句中工作两次
Python : Composite operator "+=" works twice in for statement
我有一些数据作为嵌套列表,如下所示:
(很抱歉,Whosebug 拒绝了我的原创 post,所以我将其作为图片上传。)
每个内部列表的第三个值是从0开始的整数值,
但我想通过加 1 将其转换为从 1 开始。
这些是我针对该目的及其结果的第一批代码。
代码:
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(" ")
结果:
令我惊讶的是,所有加 1 的值都增加了 2(例如 20 → 22)。
但是,当我像下面这样更改代码时,它运行良好(例如)20 → 21)。
代码:
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
结果:
哪里有漏掉的地方或者我的错误想法?谢谢你帮助我。
天哪,我发现哪里不对了。我错过了这么基本的东西,但在我找到它之前它真的是一个可怕的谜。我希望你们能理解我们经常错过这样的事情。
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(" ")
我有一些数据作为嵌套列表,如下所示:
(很抱歉,Whosebug 拒绝了我的原创 post,所以我将其作为图片上传。)
每个内部列表的第三个值是从0开始的整数值,
但我想通过加 1 将其转换为从 1 开始。
这些是我针对该目的及其结果的第一批代码。
代码:
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(" ")
结果:
令我惊讶的是,所有加 1 的值都增加了 2(例如 20 → 22)。
但是,当我像下面这样更改代码时,它运行良好(例如)20 → 21)。
代码:
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
结果:
哪里有漏掉的地方或者我的错误想法?谢谢你帮助我。
天哪,我发现哪里不对了。我错过了这么基本的东西,但在我找到它之前它真的是一个可怕的谜。我希望你们能理解我们经常错过这样的事情。
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(" ")