You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从列表移除坐标,ValueError: x not in list问题求助

移除坐标时触发ValueError问题

我在尝试从列表中移除x、y坐标时遇到问题,持续收到错误:

ValueError: list.remove(x): x not in list

我想从可用位置列表中移除已添加的坐标,但每次调用add_to_world方法都会触发这个错误,相关代码如下:

class PetriDish:
    def __init__(self, size=30):
        self.max_iterations = None
        self.SIZE = size
        self.all_positions = []
        self.available_positions = []
        self.world = self.create_world()
        self.counter = 0
        self.world_state = ['']
        self.all_cells = []

    def repr(self):
        return_string = '|'
        return_string += '-' * self.SIZE
        return_string += '|\n|'
        for i in range(self.SIZE):
            for j in range(self.SIZE):
                return_string += self.world[(i, j)]
                return_string += '|\n|'
                return_string += '-' * self.SIZE
                return_string += '|'
            return return_string

    def create_world(self):
        world = dict()
        self.all_positions = [(i, j) for i, j in product(range(self.SIZE), range(self.SIZE))]
        for position in self.all_positions:
            world[position] = empty_cell
            self.available_positions.append(position)
            return world

    def add_to_world(self, cell):
        self.world[(cell.x, cell.y)] = cell.symbol
        self.all_cells.append(cell)
        self.available_positions.remove((cell.x, cell.y))

问题根源

  1. create_world循环提前返回:遍历all_positions的循环里,第一次迭代就执行了return world,导致available_positions只添加了第一个坐标,其他网格位置根本没进列表。后续调用remove时,绝大多数坐标都不在列表里,直接触发错误。
  2. 重复移除风险:就算修复了循环,同一个坐标如果被多次调用add_to_world,第二次也会报错,因为坐标已经被移除过了。

修复方案

1. 修正create_world的循环逻辑

把return world移到循环外面,确保所有位置都被添加到available_positions:

def create_world(self):
    world = dict()
    self.all_positions = [(i, j) for i, j in product(range(self.SIZE), range(self.SIZE))]
    for position in self.all_positions:
        world[position] = empty_cell
        self.available_positions.append(position)
    return world  # 移到循环外,保证遍历完所有位置

2. 添加存在性检查避免重复移除

在执行remove前先判断坐标是否在列表中:

def add_to_world(self, cell):
    pos = (cell.x, cell.y)
    self.world[pos] = cell.symbol
    self.all_cells.append(cell)
    if pos in self.available_positions:
        self.available_positions.remove(pos)

3. 可选优化:用集合代替列表提升性能

如果频繁进行查询和移除操作,集合的效率比列表高很多(查询和删除都是O(1)时间复杂度):

# 在__init__里初始化集合
def __init__(self, size=30):
    # ... 其他代码不变
    self.available_positions = set()

# create_world里添加位置
def create_world(self):
    # ... 其他代码不变
    self.available_positions.add(position)

# add_to_world里移除
def add_to_world(self, cell):
    pos = (cell.x, cell.y)
    self.world[pos] = cell.symbol
    self.all_cells.append(cell)
    if pos in self.available_positions:
        self.available_positions.remove(pos)

内容的提问来源于stack exchange,提问作者kensi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 04:01:28