简体   繁体   English

如何使用random.shuffle获取列表形状的字典值

[英]How to get dictionary values in shape of lists with random.shuffle

I seemed to have hit a small snag. 我似乎遇到了一个小障碍。 I have a program that gets a dictionary with couple of keys and values: 我有一个程序可以获取包含几个键和值的字典:

dict = {"M" : ["L", "V", "A"], "C": ["N", "K", "W"]}

Now, I am trying to make a function that will, being provided a dictionary and a key from it, return that particular key's value, so: 现在,我正在尝试创建一个函数,该函数将提供一个字典和一个键,以返回该特定键的值,因此:

def value(dictionary, key)
   return dictionary[key]

that I managed to succeed, but the problem arises when I try to return that particular list in a random order, working with random.shuffle() . 我设法成功了,但是当我尝试使用random.shuffle()以随机顺序返回该特定列表时,就会出现问题。 It however keeps returning None . 但是,它始终返回None Anyone got any ideas? 任何人有任何想法吗?

as shuffle operates in-place, return the list after shuffle as follows: 由于随机播放是就地操作,因此在随机播放后返回列表,如下所示:

from random import shuffle
sample_dict = {"M": ["L", "V", "A"], "C": ["N", "K", "W"]}


def get_value(dictionary, key):
    a = dictionary[key]
    shuffle(a)
    return a

b = get_value(sample_dict, "M")
print b

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

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