【问题标题】:Display random choice (Python)显示随机选择(Python)
【发布时间】:2011-01-01 23:11:21
【问题描述】:

我有一个项目列表[],我想从中随机显示一个,但显示的项目在最近的 x 个请求中不得重复超过一次。

  1. list1 = item1, item2, item3, item4, 第 5 项,第 6 项,第 7 项,第 8 项,第 9 项, 第 10 项
  2. 显示随机选择 从上面的列表中
  3. list2 = 将最后显示的项目存储在 list2 中,它应该只存储 7 项目,仅此而已
  4. 随机显示 从列表中选择,但使 确定它不存在于 列表2

这是正确的做法吗?无论哪种方式,我都想知道如何将列表限制为仅存储 7 个项目?

谢谢

【问题讨论】:

标签: python list random limit


【解决方案1】:

这是你想要的吗?

list1 = range(10)
import random
random.shuffle(list1)
list2 = list1[:7]
for item in list2:
    print item
print list1[7]

换句话说,看看random.shuffle()。如果想保持原列表不变,可以复制:list_copy = list1[:]

【讨论】:

  • object = random.shuffle(list1) 返回None,它不能将值存储在对象中吗?
  • 不,random.shuffle() 就地修改列表(这就是为什么要复制的原因)。您可以通过random.choice()从列表中随机获取一项。
【解决方案2】:

collections.deque 是 python 中唯一自然支持有界的序列类型(并且仅在 Python 2.6 及更高版本中。如果使用 python 2.6 或更新版本:

# Setup
from collections import deque
from random import choice
used = deque(maxlen=7)

# Now your sampling bit
item = random.choice([x for x in list1 if x not in used])
used.append(item)

如果使用 python 2.5 或更低版本,则不能使用 maxlen 参数,并且需要再进行一次操作以切断双端队列的前面:

while len(used) > 7:
    used.popleft()

这并不是最有效的方法,但它确实有效。如果您需要速度,并且您的对象是可散列的(大多数不可变类型),请考虑使用字典作为“已使用”列表。

另外,如果你只需要这样做一次,random.shuffle 方法也可以。

【讨论】:

    【解决方案3】:

    类似:

    # Setup
    import random
    list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    list2 = []
    
    # Loop for as long as you want to display items
    while loopCondition:
        index = random.randint(0, len(list1)-1)
        item = list1.pop(index)
    
        print item
    
        list2.append(item)
        if(len(list2) > 7):
            list1.append(list2.pop(0))
    

    【讨论】:

    • 我猜无限循环只是为了演示?
    • 当然。 Nimbuz 指定他有一些“请求”进来,所以我只是将其表示为无限循环。我会修改以免有人真正保留它。:)
    【解决方案4】:

    您可以尝试使用生成器函数并在需要新项目时调用.next()

    import random
    def randomizer(l, x):
        penalty_box = []
        random.shuffle(l)
        while True:
            element = l.pop(0)
            # for show
            print penalty_box, l
            yield element
            penalty_box.append(element)
            if len(penalty_box) > x:
                # penalty time over for the first element in the box
                # reinsert randomly into the list
                element = penalty_box.pop(0)
                i = random.randint(0, len(l))
                l.insert(i, element)
    

    使用示例:

    >>> r = randomizer([1,2, 3, 4, 5, 6, 7, 8], 3)
    >>> r.next()
    [] [1, 5, 2, 6, 4, 8, 7]
    3
    >>> r.next()
    [3] [5, 2, 6, 4, 8, 7]
    1
    >>> r.next()
    [3, 1] [2, 6, 4, 8, 7]
    5
    >>> r.next()
    [3, 1, 5] [6, 4, 8, 7]
    2
    >>> r.next()
    [1, 5, 2] [4, 3, 8, 7]
    6
    >>> r.next()
    [5, 2, 6] [4, 3, 8, 7]
    1
    >>> r.next()
    [2, 6, 1] [5, 3, 8, 7]
    4
    >>> r.next()
    [6, 1, 4] [3, 8, 2, 7]
    5
    

    【讨论】:

      【解决方案5】:

      我会使用 set 对象来获取 list1 中但不在 list2 中的项目列表:

      import random
      
      list1 = set(["item1", "item2", "item3", "item4", "item5",
                   "item6", "item7", "item8", "item9", "item10"])
      list2 = []
      while True:  # Or something
          selection = random.choice(tuple(list1.difference(set(list2))))
          print(selection)
          list2.append(selection)
          if len(list2) > 7:
              list2 = list2[-7:]
      

      【讨论】:

        猜你喜欢
        • 2013-06-13
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 2014-07-09
        • 1970-01-01
        • 2014-04-23
        • 2019-05-05
        • 1970-01-01
        相关资源
        最近更新 更多