社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python:列表。remove()有线地

Jokermania • 3 年前 • 979 次点击  
def get_variables(cells):
    domains = [1,2,3,4]
    variables = {}
    for cell in cells:
        if(cell == "C11"):
            variables[cell] = [1]
        elif(cell == "C22"):
            variables[cell] = [2]
        elif(cell == "C33"):
            variables[cell] = [3]
        elif(cell == "C44"):
            variables[cell] = [4]
        else:
            variables[cell] = domains

cells = ['C'+x+y for x in "1234" for y in "1234"]
variables = get_variables(cells)
csp = CSP(variables, constraints, assigned)
pprint(csp.variables)
csp.variables["C12"].remove(1)
print(csp.variables["C13"])
output: 
{'C11': [1],
 'C12': [1, 2, 3, 4],
 'C13': [1, 2, 3, 4],
 'C14': [1, 2, 3, 4],
 'C21': [1, 2, 3, 4],
 'C22': [2],
 'C23': [1, 2, 3, 4],
 'C24': [1, 2, 3, 4],
 'C31': [1, 2, 3, 4],
 'C32': [1, 2, 3, 4],
 'C33': [3],
 'C34': [1, 2, 3, 4],
 'C41': [1, 2, 3, 4],
 'C42': [1, 2, 3, 4],
 'C43': [1, 2, 3, 4],
 'C44': [4]}
[2, 3, 4]

它应该从“C12”中删除1,而不是从“C13”中删除。为什么?我猜和记忆位置有关吧?这真让我抓狂。任何建议都将不胜感激!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131944
 
979 次点击  
文章 [ 3 ]  |  最新文章 3 年前