Python二维数组赋值问题

xiaofengeee 2010-03-07 01:44:17
RowNum=18
ColumnNum=10
SquareList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]
ActiveList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]

ActiveList[0][5]=1
SquareList[0][5]=1
结果是:
ActiveList不正常:
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]]
SquareList正常:
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
...全文
1508 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2010-03-07
  • 打赏
  • 举报
回复
其实python的=号,不是赋值而是绑定的意思,对于不可改的整型或字串,用起来就像是赋值的味道,但对于可改的序列就会有如上问题...
AKara 2010-03-07
  • 打赏
  • 举报
回复


>> for i in range(RowNum):
>> ActiveList[i]=ZeroList

ActiveList中每个下标引用到的是同一个 ZeroList 对象,当然改一个就影响所有下标了。
xiaofengeee 2010-03-07
  • 打赏
  • 举报
回复
找到原因了,但是不知道怎么回事

#这样清零后再赋值就有问题
for i in range(RowNum):
ActiveList[i]=ZeroList
ActiveList[0][5]=1
#结果:
"""
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]]
"""


#只有这样才可以
for i in range(RowNum):
for j in range(ColumnNum):
ActiveList[i][j]=0
ActiveList[0][5]=1
#结果:
"""
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
"""

谁知道怎么回事啊?
xiaofengeee 2010-03-07
  • 打赏
  • 举报
回复
贴上全部代码,刚开始学python,写俄罗斯方块

# -*- coding: utf-8 -*-
import pygame
import sys
import random
import time

#游戏框行列数
RowNum=18
ColumnNum=10
GridSize=20
#整个窗口大小
ScreenWidth=(ColumnNum+4)*GridSize+20
ScreenHeight=RowNum*GridSize
#游戏,提示窗口大小,位置,背景
LeftBox=pygame.Rect(0,0,GridSize*ColumnNum,GridSize*RowNum)
LeftbgColor=(255,255,0)
LeftfgColor=(255,0,0)
NextBox=pygame.Rect(GridSize*ColumnNum+10,10,GridSize*4,GridSize*4)
NextbgColor=(0,0,0)
#方块存储
SquareList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]
ActiveList=[[0 for a in range(ColumnNum)] for b in range(RowNum)]
CheckList=[1 for a in range(ColumnNum)]
ZeroList=[0 for a in range(ColumnNum)]
print CheckList,'\n',ZeroList
TimeSleep=0.5
class Game():
def __init__(self):
#主窗口,标题
self.screen=pygame.display.set_mode((ScreenWidth,ScreenHeight))
pygame.display.set_caption('俄罗斯方块(pygame版)')
self.background=pygame.Surface(self.screen.get_size()).convert()
#主循环
def mainloop(self):
#背景
self.screen.fill((255,255,255))
#画主游戏框
pygame.draw.rect(self.screen,LeftbgColor,LeftBox)
#画下一个游戏框
pygame.draw.rect(self.screen,NextbgColor,NextBox)
pygame.display.flip()
self.GameMoveable=True
self.RndFirst()
self.DisplayGame()
#判断循环退出
isRunning=True
while isRunning:
for event in pygame.event.get():
if event.type==pygame.QUIT:
isRunning=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
isRunning=False
if event.type==pygame.KEYDOWN and event.key==pygame.K_UP:
self.Up()
if event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN:
self.Down()
if event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
self.Left()
if event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
self.Right()
time.sleep(TimeSleep)
self.Down()
#退出循环
pygame.quit()
#显示
def DisplayGame(self):
pygame.draw.rect(self.screen,LeftbgColor,LeftBox)
pygame.display.flip()
for i in range(RowNum):
for j in range(ColumnNum):
if SquareList[i][j]==1:
pygame.draw.rect(self.screen,LeftfgColor,(j*GridSize,i*GridSize,GridSize-2,GridSize-2))
pygame.display.flip()
#检查消行
def CheckFull(self):
for i in range(RowNum):
if SquareList[i]==CheckList:
for j in range(i,0,-1):
SquareList[j]=SquareList[j-1]
SquareList[0]=ZeroList
self.DisplayGame()
#变形
def Up(self):
#print 'Up'
pass
#下落
def Down(self):
Moveable=True
for i in range(RowNum-2,-1,-1):
for j in range(ColumnNum):
if ActiveList[i][j]==1 and ActiveList[i+1][j]==0 and SquareList[i+1][j]==1:
Moveable=False
if self.GameMoveable:
for i in range(RowNum-2,-1,-1):
for j in range(ColumnNum):
if ActiveList[i][j]==1:
ActiveList[i+1][j]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i+1][j]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
print 'True\nSquareList\n',SquareList,'\nActiveList\n',ActiveList
for j in range(ColumnNum):
if ActiveList[RowNum-1][j]==1:
Moveable=False
if Moveable==False:
for i in range(RowNum):
ActiveList[i]=ZeroList
print 'ZeroActiveList\n',ActiveList
self.CheckFull()
self.RndFirst()
self.DisplayGame()
print 'False\nSquareList\n',SquareList,'\nActiveList\n',ActiveList
#左移
def Left(self):
Moveable=True
for i in range(RowNum):
for j in range(1,ColumnNum):
if ActiveList[i][j]==1 and ActiveList[i][j-1]==0 and SquareList[i][j-1]==1:
Moveable=False
for i in range(RowNum):
if ActiveList[i][0]==1:
Moveable=False
if Moveable:
for i in range(RowNum):
for j in range(1,ColumnNum):
if ActiveList[i][j]==1:
ActiveList[i][j-1]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i][j-1]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
#右移
def Right(self):
Moveable=True
for i in range(RowNum):
for j in range(ColumnNum-2,-1,-1):
if ActiveList[i][j]==1 and ActiveList[i][j+1]==0 and SquareList[i][j+1]==1:
Moveable=False
for i in range(RowNum):
if ActiveList[i][ColumnNum-1]==1:
Moveable=False
if Moveable:
for i in range(RowNum):
for j in range(ColumnNum-2,-1,-1):
if ActiveList[i][j]==1:
ActiveList[i][j+1]=ActiveList[i][j]
ActiveList[i][j]=0
SquareList[i][j+1]=SquareList[i][j]
SquareList[i][j]=0
self.DisplayGame()
def RndFirst(self):
print 'Rnd'
self.DisplayGame()
self.SetActive(0,5)

def SetActive(self,i,j):
print 'SetActive\n',ActiveList
ActiveList[i][j]=1#不正常
print SquareList,'\n',ActiveList
SquareList[i][j]=1#正常
print 'SetActive OK\n',ActiveList,'\nSquare\n',SquareList
def SetSqure(self,i,j):
pass
def main():
g=Game()
g.mainloop()

if __name__=='__main__':
main()

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧