Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建字典列表,其中每个字典包含另一个字典作为值_Python_List_Python 3.x_Dictionary - Fatal编程技术网

Python 创建字典列表,其中每个字典包含另一个字典作为值

Python 创建字典列表,其中每个字典包含另一个字典作为值,python,list,python-3.x,dictionary,Python,List,Python 3.x,Dictionary,我正在用Python编写一个算法来玩这个游戏 游戏中棋盘的当前状态是以下形式的字典: { <tile_id>: { 'counters': <number of counters on tile or None>, 'player': <player id of the player who holds the tile or None>, 'neighbo

我正在用Python编写一个算法来玩这个游戏

游戏中棋盘的当前状态是以下形式的字典:

{
    <tile_id>: {
                'counters': <number of counters on tile or None>,
                'player': <player id of the player who holds the tile or None>,
                'neighbours': <list of ids of neighbouring tile>
               },
               ...
}
我很难测试和调试我的代码,并检查它是否正常工作,因为代码只能在模拟游戏的应用程序中运行。正如您所看到的,这段代码中有很多嵌套循环,很难遵循。我现在似乎无法直接思考,因此我无法确定这场混乱,老实说,是否会如我所希望的那样发挥作用

在我写这篇文章的时候,我刚刚意识到——在代码的第7行——我只是在检查当前磁贴是否不在当前链中,因此会有交叉链,当然,这将是一个混乱。与此相反,我需要首先检查当前磁贴是否在任何链中,而不是仅在一个链中

除了这个错误,我的代码会实现我所尝试的吗?或者你能推荐一种更简单、更整洁的方法吗?(一定有!)

另外,如果我没有提供关于代码如何运行的足够信息,或者如果我需要进一步解释任何内容,例如
board
字典的内容,请告诉我

谢谢你事先的帮助

编辑:不幸的是,我在时间限制下完成了这个项目,由于这是我第一次使用Python,我目前缺乏使用下面给出的源代码优化解决方案的语言知识。这是我对这个问题的最后一个非常难看和混乱的解决方案,考虑到代码使用的数据集很小,这个解决方案最终运行得很好,效率也不是很低

for x in range(0, len(my_hexplode_chains) - 1):
    match_found = False
    for y in range(x + 1, len(my_hexplode_chains)):
        for tile_id_x, tile_x in my_hexplode_chains[x].items():             #compare each chain in list
            for tile_id_y, tile_y in my_hexplode_chains[y].items():         #to every other chain
                for neighbour in tile_x["neighbours"]:                      #if tiles in different lists
                    if neighbour == tile_id_y:                              #are neighbours
                        match_found = True
                        my_hexplode_chains[x].update(my_hexplode_chains[y]) #append one chain to the other
                        del my_hexplode_chains[y]                           #delete appended chain
                if match_found:                                             #continue loop at next chain
                    break                                                   #very ugly way to do this
            if match_found:
                break
        if match_found:
            break
    if match_found:
        break

这个优化怎么样

def find_match (my_hexplode_chains):

    x =  0
    len_chain = len(my_hexplode_chains)
    while x <= len_chain:
        y = x + 1
        for tile_id_x, tile_x in my_hexplode_chains[x].items():
            for tile_id_y, tile_y in my_hexplode_chains[y].items():
                if tile_id_y in tile_x["neighbours"]:
                    my_hexplode_chains[x].update(my_hexplode_chains[y])
                    del my_hexplode_chains[y]
                    return True
        x += 1
    return False
def find_match(my_hexplode_链):
x=0
len_链=len(我的六倍链)

而这是一个图形问题。Python中的图最容易表示为节点、调整列表对的字典。您可以非常简单地使用理解来完成这项工作。@erip不幸的是,我对Python是全新的,只是为了完成这项挑战才开始学习Python。我真的不知道你推荐的是什么,但我现在会研究一下。非常感谢(并非不幸)我很高兴看到你在学习!这是对理解的一个很好的描述。如果您熟悉数学中的集合生成器符号,它与之类似。还有一个很好的图形实现描述。不过,您需要做一些修改。您可能希望实现一个
平铺
类,并将其映射到
平铺
的列表,而不是将字符映射到字符列表。感谢您提供这两个参考资料,我现在将研究它们。然而,由于这一挑战的性质,我的所有代码都必须在一个函数中提交,因此我无法创建新的类。这是迄今为止最好、最有效的解决方案,但我相信肯定有一种不太群集的方法可以使用某种数据结构来构造它。
def find_match (my_hexplode_chains):

    x =  0
    len_chain = len(my_hexplode_chains)
    while x <= len_chain:
        y = x + 1
        for tile_id_x, tile_x in my_hexplode_chains[x].items():
            for tile_id_y, tile_y in my_hexplode_chains[y].items():
                if tile_id_y in tile_x["neighbours"]:
                    my_hexplode_chains[x].update(my_hexplode_chains[y])
                    del my_hexplode_chains[y]
                    return True
        x += 1
    return False