鱼C论坛

 找回密码
 立即注册
查看: 647|回复: 2

关于列表里面放列表的问题…

[复制链接]
发表于 2018-7-27 04:01:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我做37的的作业时候我想把鱼的位置用列表里面列表的方式打出来
空位为0 有鱼的地方放1
code如下
  1. import random, time, os

  2. scene_x = [1, 10]
  3. scene_y = [1, 10]

  4. class Fish():
  5.         def __init__(self):
  6.                 self.x = random.randint(scene_x[0], scene_x[1])
  7.                 self.y = random.randint(scene_y[0], scene_y[1])

  8.         def move(self):
  9.                 #move to new position
  10.                 new_x = self.x + random.choice([-1, 1])
  11.                 new_y = self.y + random.choice([-1, 1])
  12.                 #checking if x is over scence
  13.                 if new_x < scene_x[0]:
  14.                         self.x = self.x - (new_x - self.x)
  15.                 elif new_x > scene_x[1]:
  16.                         self.x = self.x - (new_x - self.x)
  17.                 else:
  18.                         self.x = new_x
  19.                 #checking if y is over scence
  20.                 if new_y < scene_x[0]:
  21.                         self.y = self.y - (new_y - self.y)
  22.                 elif new_y > scene_x[1]:
  23.                         self.y = self.y - (new_y - self.y)
  24.                 else:
  25.                         self.y = new_y
  26.                
  27.         def get_position(self):
  28.                 return (self.x, self.y)

  29. #make a table, 0 represent empty
  30. empty_list = [0] * 10
  31. axix = []
  32. for i in range(10):
  33.         axix.append(empty_list)
  34.         

  35. # find where the fish is, and change to 1, if position is occupied, then creat a new fish
  36. num = 0
  37. print(axix)
  38. for eachfish in fish_family:
  39.         (tem_a, tem_b) = eachfish.get_position()
  40.         tem_a = tem_a -1
  41.         tem_b = tem_b -1
  42.         if axix[tem_a][tem_b] == 0:
  43.                  axix[tem_a][tem_b] = 1

  44.            
复制代码


我实验下来
a = [[0,0,0], [0,0,0]]
a[0][1] = 1
可以得到 a = [[0,1,0], [0,0,0]]
我if的意思是这个随机编码如果是0的话就可以放鱼,否则就不行
如图是我编码的结果,明明只有10条鱼,怎么出现那么多

Screen Shot 2018-07-26 at 21.59.46.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-27 04:09:38 | 显示全部楼层
我知道原因了, 因为我以前的append放的是一个元素,他会改变
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-27 08:22:27 | 显示全部楼层
对,要好好理解append,extend的用法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-1 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表