[英]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. 我需要创建一个函数rand_dict(keys = ascii_lowercase),它返回一个字典,其中键的字母为键,键的字母排列为值。 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
: 构造
list1
和list2
时需要使用keys
:
def rand_dict(keys = string.ascii_lowercase):
list1 = list(keys)
list2 = list(keys)
np.random.shuffle(list2)
return dict(zip(list1, list2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.