簡體   English   中英

創建字典列表,其中每個字典包含另一個字典作為值

[英]Creating a list of dictionaries where each dictionary contains another dictionary as values

我正在用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>
               },
               ...
}

我有另一個字典存儲我所有的'完整'的瓷磚(即一個瓷磚比其鄰居的數量少一個,並且player是我的地方)這個字典, full_tiles ,與board字典的形式相同以上。

我現在正在嘗試創建一個列表chains ,其中列表中的每個元素都是我的完整圖塊的字典,這些圖塊與至少一個其他完整圖塊(即完整圖塊鏈)相鄰。 所以這將是我所有單獨的鏈條列表。

到目前為止,這是我的代碼:

for tile_id, tile in full_tiles.items(): #iterates through all full tiles
    current_tile = {tile_id : tile}      #temporarily stores current tile
    if not chains:                       #if chains list is empty
        chains.append(current_tile)      #begin list
    else:                                #if list is not empty
        for index, chain in enumerate(chains):            #iterate though list of chains
            if not (current_tile in chain):               #if current tile is not in current chain
                for tile_id2, tile2 in chain.items():     #iterate through tiles in current chain
                    for neighbour in tile2["neighbours"]: #iterate through each neighbour of current tile
                                                          #neighbour is in the form of tile_id
                        if neighbour in chain:            #if current tile's neighbour is in chain
                            chain[tile_id] = tile         #add tile to chain

我很難測試和調試我的代碼並檢查它是否正常工作,因為代碼只能在模擬游戲的應用程序中運行。 正如您所看到的,在這段代碼中有很多事情發生,並且所有嵌套循環都難以理解。 我似乎無法直接思考,所以我無法確定這種混亂是否能夠完全按照我的希望發揮作用。

在我寫這篇文章時,我剛剛意識到 - 在這段代碼的第7行 - 我只是檢查當前的tile是否不在當前鏈中,所以會有交叉鏈,當然,這將是一團糟。 而不是這個,我需要首先檢查當前的瓷磚是否不在任何鏈中,而不僅僅是一個。

除了這個錯誤,我的代碼會實現我的嘗試嗎? 或者你能推薦一種更簡單,更簡潔的方法嗎? (必須有!)

另外,如果我沒有提供有關代碼運行方式的足夠信息,或者我是否需要進一步解釋,例如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

您可以在游戲中的每次移動后傳遞此功能並跟蹤輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM