【发布时间】:2021-04-03 06:31:30
【问题描述】:
假设我有一个这样的列表字典:
lists= {'pet': ["dog", "cat", "parrot"],
'amount': [1,2,3],
'food': ["canned", "dry"]}
我想从每个列表中随机选择一个元素,所以结果是这样的:
{'amount': 2, 'food': 'canned', 'pet': 'cat'}
使用 Python 实现它的最简单方法是什么?
【问题讨论】:
-
使用随机模块,遍历字典,并从每个值中选择一个随机元素?
标签: python python-3.x list dictionary random