简体   繁体   English

如何从字典中的列表之一中随机选择一项,并获取列表的键?

[英]How do I randomly pick an item from one of the lists inside a dictionary, and also get the key for the list?

I have a dictionary of lists: 我有一个列表字典:

lists=dict(animals=["dog","cat","shark"],
           things=["desk","chair","pencil"],
           food=["spaghetti","ice-cream","potatoes"])

How can I get Python to randomly pick an item from one of the lists and tell me what list it was in? 如何让Python从列表中的一个中随机选择一个项目并告诉我它在哪个列表中? Or how do I pick a key from the dictionary and then a value from the list corresponding to that key? 或者如何从字典中选择一个键,然后从对应于该键的列表中选择一个值?

For example: 例如:

dog - from animals
potatoes - from food

random.choice picks a random item from a sequence: random.choice从序列中选择一个随机项目:

import random

Select the key to draw from your dict , which you've named lists : 选择要从dict绘制的键,您将其命名为lists

which_list = random.choice(lists.keys())

Then, use that key to get a list from the dict : 然后,使用该键从dict获取list

item = random.choice(lists[which_list])

If you need equal weighting: 如果需要相等的权重:

import random

which_list, item = random.choice([(name, value) 
                                     for name, values in lists.iteritems() 
                                         for value in values])

Two approaches I can think of right away: 我可以立即想到两种方法:

  • pick a list name (key) first, then pick an entry from it -- you'll just have to be careful if the lists have different lengths, if you want an uniform distribution 首先选择一个列表名称(键),然后从中选择一个条目-如果列表的长度不同,则只需要小心,如果您想要统一分配
  • flatten the dict of lists into one list of ('list-name','value') pairs (easier to get the uniform distribution right no matter how many entries there are per list, but requires more memory) 将列表的字典展平为('list-name','value')对的一个列表('list-name','value')无论每个列表有多少个条目,都更容易获得统一的分配权,但是需要更多的内存)

One way to do the former: 前一种方法:

from itertools import chain
import random
weight_choices = list(chain(*([name] * len(values) for (name, values) in lists.iteritems()))) # generate a list of the form ("animals", "animals", "animals", ...)
list_name = random.choice(weight_choice) # The list it's chosen from...
chosen_item = random.choice(lists[list_name]) # and the item itself

(and if you don't care about getting an uniform distribution between lists:) (如果您不在乎列表之间的均匀分布:)

import random
list_name = random.choice(lists.keys())
chosen_item = random.choice(lists[list_name])

... and the latter method: ...以及后一种方法:

from itertools import chain, repeat
all_items = list(chain(*((zip(repeat(name), values) for (name, values) in lists.iteritems()))))
list_name, chosen_item = random.choice(all_items)

and a less itertools way for the latter: 而后者的itertools方法较少:

all_items = []
for name, values in lists.iteritems():
  for value in values:
    all_items.append((name, value))
list_name, chosen_item = random.choice(all_items)

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

相关问题 如何从字典中随机选择一个键 - How to pick one key from a dictionary randomly 如何从字典内的随机列表中选择随机项目 - How to pick random item from random list inside a dictionary 如何从字典的每个列表中随机选择一个项目? - How to randomly select one item from every list of a dictionary? 如果特定键项在字典中,如何获取列表中字典的索引 - How to get the index of a dictionary inside list if a specific key item is in the dictionary 如何从字典的每个值列表中选择一项来获得所有排列? - How do I get all the permutations selecting one item from each value list in a dictionary? 如何随机选择列表中不包括一种可能性的项目? - How to randomly pick an item in a list excluding one possiblity? Python:如何从字典键中随机选择一个值? - Python: How do I randomly select a value from a dictionary key? 如何将多个列表附加到python字典中的一个键? - How do I append multiple lists to one key in a python dictionary? 如何使用 .get() 方法获取字典内列表中的项目? - How do I use the .get() method to get an item in a list that is inside a dictionary? 我想在我的代码中实现一个功能,该功能将从列表中随机选择键和值,以便稍后将它们分配给字典 - I want to implement a feature in my code that will randomly pick keys and values from lists to later assign them to a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM