简体   繁体   中英

Randomly print a dictionary key, then compare its value with another dictionary

I have a problem with printing a random key from a dictionary. With the random of dict_2 everything works correctly, and the function also works correctly, but with the random of dict_1 there is something wrong.

The purpose of this code is to randomly select the key of dict_1 and dict_2 , "compare" them grammatically using the function (the keys of dict_1 and dict_2 must have the same gender and unit) and then print them. The function works correctly

IMPORTANT: I cannot use the same code I used for dict_2 , because otherwise I will get a key WITHOUT its values, so it cannot be "compared" to dict_2 using the get_example function. Even using list(dict_1.items()) and not list(dict_1.keys()) , I can't compare values with the other dictionary. Instead, currently, the values are compared correctly, but there is no randomization of dict_1 . I am making several attempts, but I get so many different errors, too many different errors.

How can I randomly get a dict_1 key? I always print only the "archiviato" or "archiviata" key of dict_1 , but I would like to print a random one among all those of dict_1. I know the solution is very simple, but I can't. All the rest of the code works fine. Who can help me?

I have split the dictionary and code for cleanliness and clarity, but below is the complete code.

This is my small code:

import random

#Random Dict_2
x = list(dict_2.keys()) 
x_random = random.choice(x)

#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
    for key, value in dict_1.items():
        if dict_2[x_random] == value:
            return key

function = get_example(x_random)

test = f"{function} {x_random}"
print(test)

This is dictionary:

dict_1 = {

            #Male
            "archiviato " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "gestito " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },
                      
            "smarrito " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            #Female
            "archiviata " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
      
            "gestita " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
             
            "smarrita " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },      
        }
    
dict_2 = {

            #Male
            "il documento" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "il foglio" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "lo scatolone" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },


            #Female        
            "la cartella" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
                      
            "la borsa" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },

            "la chiave" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
                      
        }

This is code complete:

import random

dict_1 = {

            #Maschili
            "archiviato " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "gestito " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },
                      
            "smarrito " : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

                  
        
            #Femmili
            "archiviata " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
      
            "gestita " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
             
            "smarrita " : {
            "genere" : "femminile",
            "unità" : "singolare",
            },      
        }



dict_2 = {

            #Maschili
            "il documento" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "il foglio" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },

            "lo scatolone" : {
            "genere" : "maschile",
            "unità" : "singolare",
            },


            #Femminili        
            "la cartella" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
                      
            "la borsa" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },

            "la chiave" : {
            "genere" : "femminile",
            "unità" : "singolare",
            },
                      
        }


#Random Dict_2
x = list(dict_2.keys()) 
x_random = random.choice(x)

#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
    for key, value in dict_1.items():
        if dict_2[x_random] == value:
            return key

function = get_example(x_random)

test = f"{function} {x_random}"
print(test)

Rather than returning as soon as you find a match, I would suggest keeping track of all matches, then picking one of the matches at random.

Example:

import random

#Random Dict_2
x = list(dict_2.keys()) 
x_random = random.choice(x)

#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
    matches = []
    for key, value in dict_1.items():
        if dict_2[x_random] == value:
            matches.append(key)
    # If matches has no elements here, this will cause an error
    return random.choice(matches)

function = get_example(x_random)

test = f"{function} {x_random}"
print(test)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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