简体   繁体   English

根据值更新字典键

[英]Update dictionary key based on value

I have the following dictionary: 我有以下字典:

players = {'Roger': ['player1', 'dA'], 'Luka': ['player2', 'sK']}. 

I want to update the key that contains 'player2' , but I can't update players [Luka] because 'player2' is not always Luka . 我想更新包含'player2'的密钥,但是我无法更新玩家[Luka]因为'player2'并不总是Luka How can I select keys linked to 'player2' ? 如何选择链接到'player2'键?

(for those wondering, dA = Ace of Diamonds , sK = King of Spades . These values will also be different every time). (对于那些想知道的人, dA = Ace of DiamondssK = King of Spades 。这些值每次都不同。)

Edit: Here's the part of my code: (It won't run because I left out a lot of clutter) 编辑:这是我的代码的一部分:(它将不会运行,因为我遗漏了很多混乱)

qPlayers = 2  #Amount of players
def game(qPlayers):
    players[nr]["value"].append(new_card)
    deal_to =[]
    for player in players:
        deal_to.append(player)
    deal(qPlayers,deck,players,deal_to)

def setup(qPlayers):
    playerhands = []
    for x in range(1,qPlayers+1):
        player = {}
        player["name"] = input("Enter your name, player {}\n>>>".format(x))
        playerhands.append(player)
    return playerhands      


def deal(qPlayers,deck,players,deal_to):
    nr = -1
    for player in deal_to:
        nr +=1
        new_card = getCard(deck)   #getCard produces a random card like sA, k9, cQ

You can keep track of which key in the dict contains your player2 value, or else you will have to iterate through the dict until you find a key that does. 您可以跟踪dict中的哪个key包含您的player2值,否则您将不得不遍历该dict直到找到一个包含该key为止。 If you frequently need to search based on something internal to the value , you may want to reconsider the data structure you are using to store this data. 如果您经常需要根据value内部的内容进行搜索,则可能需要重新考虑用于存储该数据的数据结构。

def findKeyWithValue(value):
    for playerName, playerData in players.items():
        if value in playerData:
            # your code here

You have two options here: 您在这里有两个选择:

  1. Search through the entire directory, checking for the value which contains 'player2'. 搜索整个目录,检查包含“ player2”的值。 This ends up being inefficient, and makes the dictionary structure somewhat useless. 这最终效率低下,并使字典结构变得毫无用处。

  2. Use a list instead of a dictionary, and model your data accordingly, similar to the example below. 使用列表而不是字典,并对数据进行相应的建模,类似于下面的示例。

An advantage of this data structure is that you don't need the redundant player1 / player2 identifiers, since the list index provides that implicitly. 这种数据结构的一个优点是您不需要冗余的player1 / player2标识符,因为列表索引隐式地提供了该标识符。 To reference player2 you'd take the second element from the list ( players[1] since indexing starts at 0). 要引用player2您需要从列表中选择第二个元素(因为索引从0开始,所以是players[1] )。

players = [
    {'Name' : 'Roger', 'Value' : 'dA'},
    {'Name' : 'Luka', 'Value' : 'sK'}
]

You can iterate the dictionary to find the key to update. 您可以迭代字典以找到要更新的密钥。 dict.iteritems() will do that job in python 2.7. dict.iteritems()将在python 2.7中完成该工作。

players = {'Roger': ['player1', 'dA'], 'Luka': ['player2', 'sK']}

for key, value in players.iteritems():
    if value[0] == "player2":
        players[key][1] = "sQ"

print players

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM