目录
Get random key and value from a dictionary in Python
- 从 Python 字典中获取随机键和值
- 从 Python 中的字典中获取随机键
- 从 Python 字典中获取随机值
- 从 Python 中的字典中获取多个随机键和值
- 使用 random.choices() 从字典中获取多个随机键和值
在 Python 中从字典中获取随机键和值
从字典中获取随机键值对:
- 使用该
dict.items()
方法获取字典项目的视图。 - 使用该类
list()
将视图转换为列表。 - 使用该
random.choice()
方法从字典中获取随机键值对。
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # ✅ get random key-value pair from dictionary key, value = random.choice(list(my_dict.items())) print(key, value) # 👉️ name Borislav Hadzhiev # ----------------------------------------------- # ✅ get random key from dictionary key = random.choice(list(my_dict)) print(key) # 👉️ topic # ----------------------------------------------- # ✅ get random value from dictionary value = random.choice(list(my_dict.values())) print(value) # 👉️ bobbyhadz.com
dict.items
方法返回字典项((键,值)对)的新视图。
my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ dict_items([('name', 'Borislav Hadzhiev'), ('fruit', 'apple'), ('number', 5), ('website', 'bobbyhadz.com'), ('topic', 'Python')]) print(my_dict.items())
list()
将键值对的视图转换为列表random.choice()
。列表类接受一个可迭代对象并返回一个列表对象。
random.choice方法接受一个序列并从
非空序列返回一个随机元素。
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } key, value = random.choice(list(my_dict.items())) print(key, value) # 👉️ website bobbyhadz.com
如果序列为空,则该random.choice()
方法引发一个IndexError
.
在 Python 中从字典中获取随机键
这是一个两步过程:
- 使用该类
list()
将字典转换为键列表。 - 使用该
random.choice()
方法从列表中获取随机密钥。
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } key = random.choice(list(my_dict)) print(key) # 👉️ topic
我们使用该类list()
将字典转换为键列表。
my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ ['name', 'fruit', 'number', 'website', 'topic'] print(list(my_dict)) # 👇️ ['name', 'fruit', 'number', 'website', 'topic'] print(list(my_dict.keys()))
dict.keys()
方法来更明确。dict.keys方法返回字典键的新视图。
最后一步是将密钥列表传递给random.choice()
方法以获取随机密钥。
在 Python 中从字典中获取一个随机值
这是一个三步过程:
- 使用该
dict.values()
方法获取字典值的视图。 - 使用该类
list()
将视图对象转换为列表。 - 使用该
random.choice()
方法从字典中获取一个随机值。
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } value = random.choice(list(my_dict.values())) print(value) # 👉️ Borislav Hadzhiev
dict.values方法返回字典值的新视图。
import random my_dict = { 'name': 'Borislav Hadzhiev', 'fruit': 'apple', 'number': 5, 'website': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ dict_values(['Borislav Hadzhiev', 'apple', 5, 'bobbyhadz.com', 'Python']) print(my_dict.values())
我们使用该类list()
将视图对象转换为列表,并使用该
random.choice()
方法从列表中获取随机值。
从 Python 字典中获取多个随机键和值
如果您需要从字典中获取多个随机键和值,请使用该
random.sample()
方法。
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.sample(list(my_dict), 2) print(random_keys) # 👉️ ['age', 'name'] random_values = random.sample(list(my_dict.values()), 2) print(random_values) # 👉️ [30, 'Bobbyhadz'] random_items = random.sample(list(my_dict.items()), 2) print(random_items) # 👉️ [('country', 'Chile'), ('id', 1)]
random.sample方法返回从提供的序列中选择的 N个
唯一元素的列表。
list()
请注意,在对 的调用中,我们使用该类将字典的键、值和项目转换为列表random.sample()
。
我们不能直接将字典传递给该random.sample()
方法。
# Get multiple random keys and values from a Dictionary using random.choices()
You can also use the random.choices()
method to get multiple random keys and
values from a dictionary.
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.choices(list(my_dict), k=2) print(random_keys) # 👉️ ['name', 'country'] random_values = random.choices(list(my_dict.values()), k=2) print(random_values) # 👉️ ['Santiago', 30] random_items = random.choices(list(my_dict.items()), k=2) print(random_items) # 👉️ [('id', 1), ('city', 'Santiago')]
The
random.choices
method returns a k
sized list of elements chosen from the provided iterable
with replacement.
If the iterable is empty, the method raises an IndexError
exception.
import random my_dict = { 'id': 1, 'name': 'Bobbyhadz', 'age': 30, 'country': 'Chile', 'city': 'Santiago', } random_keys = random.choices(list(my_dict), k=3) print(random_keys) # 👉️ ['city', 'city', 'name'] random_values = random.choices(list(my_dict.values()), k=3) print(random_values) # 👉️ ['Santiago', 1, 'Santiago'] random_items = random.choices(list(my_dict.items()), k=3) # 👇️ [('age', 30), ('name', 'Bobbyhadz'), ('name', 'Bobbyhadz')] print(random_items)
Notice that when using the random.choices()
method it is possible to get
repeated values.
This is not the case when using the random.sample()
method.
The random.sample()
method can never return repeated values.
# Additional Resources
您可以通过查看以下教程来了解有关相关主题的更多信息: