简体   繁体   中英

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() . It however keeps returning 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

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