简体   繁体   中英

Creating a function that outputs a dictionary with random values

I need to create a function rand_dict(keys=ascii_lowercase) that that returns a dictionary with the letters of keys as keys and a permutation of the letters of keys as values. Here is what i got

import string
import numpy as np

keys= 3

def rand_dict(keys):
    list1 = list(string.ascii_lowercase)
    list2 = list(string.ascii_lowercase)
    np.random.shuffle(list2)
    return dict(zip(list1, list2))

print(rand_dict(keys))

I get the correct output - however as you can see, i am not using the keys value anywhere. How do I setup the function to call it as needed.

You need to use keys when constructing list1 and list2 :

def rand_dict(keys = string.ascii_lowercase):
    list1 = list(keys)
    list2 = list(keys)
    np.random.shuffle(list2)
    return dict(zip(list1, list2))

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