[英]Python range is printed twice
I am currently working on a project (In Python 2.7), and ran into a problem when the for
values for range()
was printed twice.我目前正在开发一个项目(在 Python 2.7 中),当
range()
的for
值被打印两次时遇到了问题。 I minimized a lot of the part I am talking about, so if just assume that there are other parts in my code.我最小化了很多我正在谈论的部分,所以如果只是假设我的代码中还有其他部分。
So, basically, I have a 5 by 5 board, and depending on case1
and case2
, I am copying the board with list()
and making alters to it.所以,基本上,我有一个 5 x 5 的板,根据
case1
和case2
,我正在使用list()
复制板并对其进行更改。 Then, I want to have a column of numbers stating the row numbers, so that the output will be something like:然后,我想要一列数字说明行号,这样 output 将类似于:
1 O O O O O
2 O O O O O
3 O O O O O
4 O O O O O
5 O O O O O
for case1
and case2
.对于
case1
和case2
。
I have:我有:
board = []
# Creating board
for i in range(5):
board.append(["O"] * 5)
case1 = True
case2 = True
if case1 == True:
board1 = list(board) #Making duplicate of board
# I took out a lot of the code making alterations to board1
for i in range(5):
board1[i].insert(0, str(i + 1) + " ")
if case2 == True:
board2 = list(board) #Making duplicate of board
# I took out a lot of the code making alterations to board2
for i in range(5):
board2[i].insert(0, str(i + 1) + " ")
#Printing the boards
for i in range(5):
print " ".join(board1[i])
print "\n\n" #Just a gap
for i in range(5):
print " ".join(board2[i])
However, the output is:但是,output 是:
1 1 O O O O O
2 2 O O O O O
3 3 O O O O O
4 4 O O O O O
5 5 O O O O O
for each board1
and board2
.对于每个
board1
和board2
。 I get the expected result when I take out everything concerning case2
, however, with my current code, it is printing the range() for the rows printing twice.当我取出有关
case2
的所有内容时,我得到了预期的结果,但是,使用我当前的代码,它正在打印两次打印行的 range() 。
I would like to know if there are ways to solve this problem.我想知道是否有办法解决这个问题。
Sorry if my wording is very unclear.对不起,如果我的措辞很不清楚。 I would be thankful if somebody to edited the question/title for more clarity.
如果有人为了更清楚而编辑问题/标题,我将不胜感激。
As @Random Davis indicated, the problem comes from the fact that you copy a list of lists, in order to create board1 and board2.正如@Random Davis 指出的那样,问题出在您复制列表列表以创建 board1 和 board2 的事实。
board1=list(board)
board2=list(board)
This way, when you insert the values 1-5 to board1, they will also be inserted in board2, as both are still references to board, therefore when you insert the values in board2, they are inserted for second time.这样,当您将值 1-5 插入到 board1 时,它们也会插入到 board2 中,因为它们仍然是对 board 的引用,因此当您在 board2 中插入值时,它们会被第二次插入。
To avoid this, you must create board1, board2 as below:为避免这种情况,您必须创建 board1、board2,如下所示:
board1=[list(i) for i in board]
board2=[list(i) for i in board]
And everything will work properly一切都会正常工作
Check here why changes to board1 reflect to board2:在这里检查为什么对 board1 的更改会反映到 board2:
Copying nested lists in Python 在 Python 中复制嵌套列表
List changes unexpectedly after assignment. 分配后列表意外更改。 How do I clone or copy it to prevent this?
如何克隆或复制它以防止这种情况发生?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.