2010-01-06 146 views
1

我有一个列表[]我想随机显示一个项目,但显示的项目在最后的x个请求中不能重复多次。显示随机选择(Python)

  1. list1的= ITEM1,ITEM2,项目3,ITEM4, ITEM5,ITEM6,item7,item8,item9, 项目10
  2. 显示从上述列表中
  3. list2中=存储中的最后一个随机选择 在列表2显示项目只应店7个 项目,而不是更多
  4. 显示从列表中随机选择 但要 确保它不会在 list2中存在

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

谢谢

+0

重复:HTTP://计算器。 COM /问题/ 1058712 /如何-DO-I-选择 - 一个随机元素从 - 一个阵列式-蟒。可能是家庭作业。 – 2010-01-06 11:11:05

回答

7

collections.deque是在自然地支持定界蟒的唯一序列类型(并且仅在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方法也可以。

+0

有关deque的了解,谢谢!:) – 3zzy 2010-01-06 08:47:22

4

这是你想要的吗?

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[:]

+0

object = random.shuffle(list1)返回None,它不能将值存储在一个对象中吗? – 3zzy 2010-01-06 08:31:11

+0

不,'random.shuffle()'就地修改列表(这就是为什么要复制的原因)。你可以通过'random.choice()'从列表中获得一个随机项目。 – 2010-01-06 08:35:22

1

喜欢的东西:

# 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)) 
+2

*无限循环*仅用于演示我猜? – 2010-01-06 08:19:36

+1

绝对如此。 Nimbuz指出他有一些“请求”进来,所以我只是将它表示为一个无限循环。我会修改,以免有人真的把它放进去。:) – Sapph 2010-01-06 08:27:36

2

你可以尝试使用发电机的功能,并呼吁.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 
1

我会用一组对象来获得在列表2 List1中的项目清单,但并不:

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:]