我如何从字典中得到一个随机对?我正在制作一款游戏,玩家需要猜测一个国家的首都,我需要随机出现问题。

字典看起来像{'VENEZUELA':'CARACAS'}

我该怎么做呢?


当前回答

我假设你在做一个类似测验的应用程序。对于这种应用程序,我写了一个函数,如下所示:

def shuffle(q):
"""
The input of the function will 
be the dictionary of the question
and answers. The output will
be a random question with answer
"""
selected_keys = []
i = 0
while i < len(q):
    current_selection = random.choice(q.keys())
    if current_selection not in selected_keys:
        selected_keys.append(current_selection)
        i = i+1
        print(current_selection+'? '+str(q[current_selection]))

如果我将输入问题={'委内瑞拉':'加拉加斯','加拿大':'多伦多'},并调用函数shuffle(问题),那么输出将如下:

VENEZUELA? CARACAS
CANADA? TORONTO

您还可以通过调整选项进一步扩展此功能

其他回答

下面是一个字典类的Python代码,它可以在O(1)时间内返回随机键。(为了可读性,我在代码中包含了myypy类型):

from typing import TypeVar, Generic, Dict, List
import random

K = TypeVar('K')
V = TypeVar('V')
class IndexableDict(Generic[K, V]):
    def __init__(self) -> None:
        self.keys: List[K] = []
        self.vals: List[V] = []
        self.dict: Dict[K, int] = {}

    def __getitem__(self, key: K) -> V:
        return self.vals[self.dict[key]]

    def __setitem__(self, key: K, val: V) -> None:
        if key in self.dict:
            index = self.dict[key]
            self.vals[index] = val
        else:
            self.dict[key] = len(self.keys)
            self.keys.append(key)
            self.vals.append(val)

    def __contains__(self, key: K) -> bool:
        return key in self.dict

    def __len__(self) -> int:
        return len(self.keys)

    def random_key(self) -> K:
        return self.keys[random.randrange(len(self.keys))]

如果你不想使用随机模块,你也可以尝试popitem():

>> d = {'a': 1, 'b': 5, 'c': 7}
>>> d.popitem()
('a', 1)
>>> d
{'c': 7, 'b': 5}
>>> d.popitem()
('c', 7)

因为字典不保留顺序,所以使用popitem可以从字典中获得任意(但不是严格随机)顺序的项。

还要记住,popitem从字典中删除键值对,正如文档中所述。

Popitem()用于对字典进行破坏性迭代

试试这个:

import random
a = dict(....) # a is some dictionary
random_key = random.sample(a, 1)[0]

这绝对有效。

我需要遍历字典中的键的范围,而不是每次都对它排序,并找到了Sorted Containers库。我发现这个库可以通过索引随机访问字典项,直观地解决了这个问题,而不用每次都遍历整个字典:

>>> import sortedcontainers
>>> import random
>>> d = sortedcontainers.SortedDict({1: 'a', 2: 'b', 3: 'c'})
>>> random.choice(d.items())
(1, 'a')
>>> random.sample(d.keys(), k=2)
[1, 3]

我只是偶然发现了一个类似的问题,并设计了以下解决方案(相关函数是pick_random_item_from_dict;其他函数只是为了完整性)。

import random


def pick_random_key_from_dict(d: dict):
    """Grab a random key from a dictionary."""
    keys = list(d.keys())
    random_key = random.choice(keys)
    return random_key


def pick_random_item_from_dict(d: dict):
    """Grab a random item from a dictionary."""
    random_key = pick_random_key_from_dict(d)
    random_item = random_key, d[random_key]
    return random_item


def pick_random_value_from_dict(d: dict):
    """Grab a random value from a dictionary."""
    _, random_value = pick_random_item_from_dict(d)
    return random_value

# Usage
d = {...}
random_item = pick_random_item_from_dict(d)

与之前的答案的主要区别在于我们使用list(d.s items())处理字典拷贝的方式。我们可以通过只复制d.s keys()并使用随机键来选择其关联值并创建随机项来部分避免这种情况。