【问题标题】:Python pick 20 random results from list [duplicate]Python从列表中挑选20个随机结果[重复]
【发布时间】:2016-10-03 04:59:32
【问题描述】:

我想从以下列表中获得 20 个随机结果:

coordinates = [
  [20, 140], [40, 140], [60, 140], [80, 140], [100, 140], [120, 140],
  [20, 120], [40, 120], [60, 120], [80, 120], [100, 120], [120, 120],
  [20, 100], [40, 100], [60, 100], [80, 100], [100, 100], [120, 100],
  [20, 80], [40, 80], [60, 80], [80, 80], [100, 80], [120, 80],
  [20, 60], [40, 60], [60, 60], [80, 60], [100, 60], [120, 60],
  [20, 40], [40, 40], [60, 40], [80, 40], [100, 40], [120, 40]
]

我试过random.shuffle,但它返回None

【问题讨论】:

  • 试过了但没用。
  • 请阅读stackoverflow.com/help/how-to-ask。向我们展示无效的代码。
  • 当我使用 random.shuffle(coordinaten) 它返回 None。
  • 还有 random.choice() 它只返回一个结果,我需要 20 个结果在一个新列表中
  • random.shuffle 什么也不返回。他直接打乱你的源参数

标签: python random


【解决方案1】:

random.shuffle直接打乱输入参数,不返回任何内容。

random.shuffle:

import random
random.shuffle(coordinates)[:20]

但它会更改您的源列表。我的意见:不好。

你可以使用random.sample

import random
random.sample(coordinates, 20)

【讨论】:

    【解决方案2】:

    一种方法是使用random.shuffleshuffle 没有返回一个新的随机列表,而是在原地修改列表,这就是它没有返回任何内容的原因。

    from random import shuffle
    shuffle(coordinates)
    result = coordinates[:20]
    

    【讨论】:

      【解决方案3】:

      我认为您可能正在从 random 库中寻找 random.sample。你可以这样使用:

      import random
      my_new_list = random.sample(coordinates, 20)
      

      【讨论】:

        【解决方案4】:

        如果您希望随机排列 20 个 唯一 值,请使用 random.sample():

        random.sample(coordinates, 20)
        
        random.sample(population, k)¶
        

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

        >>> random.sample(coordinates, 20)
        [[80, 60], [40, 100], [80, 100], [60, 80], [60, 100], [40, 60], [40, 80], [80, 120], [120, 140], [120, 100], [100, 80], [40, 120], [80, 140], [100, 140], [20, 80], [120, 80], [100, 100], [20, 40], [120, 120], [100, 120]]
        

        您可以使用random.choice() 20 次,但这不会是“唯一的”——元素可能会重复,因为每次都是随机选择的:

        >>> [random.choice(coordinates) for _ in range(20)]
        [[80, 80], [40, 140], [80, 140], [60, 60], [120, 100], [20, 120], [100, 80], [120, 100], [20, 60], [100, 120], [100, 40], [80, 80], [100, 80], [80, 120], [20, 40], [100, 80], [60, 80], [80, 140], [40, 40], [120, 40]]
        

        【讨论】:

        • 如果您需要随机选择替换,请使用random.choices(Python 3.6 中的新功能)而不是在列表理解中使用random.choice
        【解决方案5】:

        试试下面的食谱:

        coordinaten = [[20, 140], [40, 140], [60, 140], [80, 140], [100, 140], [120, 140],
                   [20, 120], [40, 120], [60, 120], [80, 120], [100, 120], [120, 120],
                   [20, 100], [40, 100], [60, 100], [80, 100], [100, 100], [120, 100],
                   [20, 80], [40, 80], [60, 80], [80, 80], [100, 80], [120, 80],
                   [20, 60], [40, 60], [60, 60], [80, 60], [100, 60], [120, 60],
                   [20, 40], [40, 40], [60, 40], [80, 40], [100, 40], [120, 40]]
        
        import random
        
        print(random.sample(coordinaten, 20))
        

        【讨论】:

          猜你喜欢
          • 2010-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-19
          • 2012-01-12
          • 2013-02-08
          • 1970-01-01
          相关资源
          最近更新 更多