【问题标题】:Is there a way to retrieve multiple random values from a list? [duplicate]有没有办法从列表中检索多个随机值? [复制]
【发布时间】: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)}’)

我被困在这里了。任何帮助表示赞赏。

【问题讨论】:

    标签: python discord.py-rewrite


    【解决方案1】:

    Python 提供了 random.sample(),它完全可以满足您的需求。

    random.sample(population, k)
    

    返回从种群序列或集合中选择的唯一元素的 k 长度列表。用于无放回的随机抽样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2020-12-27
      • 2019-10-29
      相关资源
      最近更新 更多