【发布时间】:2020-09-29 06:14:24
【问题描述】:
我在 discord.py 中编写了一个不和谐机器人,我希望人们能够从一个列表中检索多个不同的随机值。这是一个例子
list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
@client.command
async def pick(ctx, amount):
await ctx.send(f’{random.choice(list)}’)
我被困在这里了。任何帮助表示赞赏。
【问题讨论】:
我在 discord.py 中编写了一个不和谐机器人,我希望人们能够从一个列表中检索多个不同的随机值。这是一个例子
list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
@client.command
async def pick(ctx, amount):
await ctx.send(f’{random.choice(list)}’)
我被困在这里了。任何帮助表示赞赏。
【问题讨论】:
Python 提供了 random.sample(),它完全可以满足您的需求。
random.sample(population, k)返回从种群序列或集合中选择的唯一元素的 k 长度列表。用于无放回的随机抽样。
【讨论】: