Home > front end >  Python how to avoid a repeated dictionary key
Python how to avoid a repeated dictionary key

Time:11-26

So, I was trying to make a function to distribute tokens from a dictionary that works as a data base to other blank dictionaries. I use the random.randint() to obtain random tokens out of the big dict and transferred them to the other dict until they are filled with a len(dict) == 7. The problem occurs when the random number generated repeats itself and the dictionary doesn't add some tokens since its repeating the same key.

I want to know how would you avoid the random number to repeat itself or instead of not adding the element to add the next (key,value).

Import random

L_TOKENS = ["P.0|0","P.0|1","P.0|2", "P.0|3", "P.0|4", "P.0|5", "P.0|6", "P.1|1",
    "P.1|2" , "P.1|3", "P.1|4", "P.1|5", "P.1|6",
    "P.2|2", "P.2|3", "P.2|4", "P.2|5", "P.2|6",
    "P.3|3", "P.3|4", "P.3|5", "P.3|6",
    "P.4|4", "P.4|5", "P.4|6","P.5|5", "P.5|6","P.6|6"
]

TOKENS = {
    "P.0|0": [0,0], "P.0|1": [0,1], "P.0|2": [0,2], "P.0|3": [0,3], "P.0|4": [0,4], "P.0|5": [0,5], "P.0|6": [0,6],
    "P.1|1": [1,1], "P.1|2": [1,2], "P.1|3": [1,3], "P.1|4": [1,4], "P.1|5": [1,5], "P.1|6": [1,6],
    "P.2|2":[2,2], "P.2|3":[2,3], "P.2|4":[2,4], "P.2|5":[2,5], "P.2|6":[2,6],
    "P.3|3":[3,3], "P.3|4":[3,4], "P.3|5":[3,5], "P.3|6":[3,6],
    "P.4|4":[4,4], "P.4|5":[4,5], "P.4|6":[4,6],
    "P.5|5":[5,5], "P.5|6":[5,6],
    "P.6|6":[6,6]
}


TOKENS_JP = {} #Player's tokens
TOKENS_CPU = {} # CPU's tokens



def distribute():
    for j in range(7): #Number of times a player receives a token
        r = random.randint(0,27)
        for keys in TOKENS:
            if L_TOKENS[r] == keys:
                TOKENS_JP[clave] = TOKENS[clave]  #The player receives a token
                   
    for i in range(7):            
        for keys in TOKENS:
            r = random.randint(0,27) 
            if L_TOKENS[r] not in TOKENS_JP:   #The CPU receives a token the player doesn't have.
                if L_TOKENS[r] == keys:
                    TOKENS_CPU[keys] = TOKNES[key]
             
    print(TOKENS_JP)
    print(len(TOKENS_JP))

    print(TOKENS_CPU)
    print(len(TOKENS_CPU))

distribute()

And I get the following result:
{'P.0|3': [0, 3], 'P.3|5': [3, 5], 'P.2|5': [2, 5], 'P.2|6': [2, 6], 'P.2|2': [2, 2], 'P.3|6': [3, 6]}
6
{'P.1|3': [1, 3], 'P.0|6': [0, 6], 'P.0|0': [0, 0], 'P.0|1': [0, 1], 'P.0|5': [0, 5]}
5

But I wish for it to look like this:
{'P.0|3': [0, 3], 'P.3|5': [3, 5], 'P.2|5': [2, 5], 'P.2|6': [2, 6], 'P.2|2': [2, 2], 'P.3|6': [3, 6], 'P.4|4':[4,4]}
7
{'P.1|3': [1, 3], 'P.0|6': [0, 6], 'P.0|0': [0, 0], 'P.0|1': [0, 1], 'P.0|5': [0, 5], 'P.6|6':[6,6], 'P.2|4':[2,4}
7

CodePudding user response:

To randomly select without repetition, you could instead use random.sample(), which returns n unique elements. We can take all the needed elements with this—7 tokens for the user, plus 7 tokens for the cpu.

Since the results are random, it doesn't matter the order that they're selected, so we can take the first 7 tokens for the user and the next 7 tokens for the cpu:

selected_tokens = random.sample(L_TOKENS, 14)

player_tokens = selected_tokens[0:7]
cpu_tokens = selected_tokens[7:14]

Then, for selecting the keys, you can do:

TOKENS_JP = {key: TOKENS[key] for key in player_tokens}
TOKENS_CPU = {key: TOKENS[key] for key in cpu_tokens}
  • Related