一遍又一遍地循环通过列表

时间:2014-07-29 13:01:34

标签: python

列表players包含类Players的实例。我想为每个循环增加列表的索引1,但是在它到达最后一个元素之后,它应该再次从0开始。

基本上,这是一个扑克代码,我有X个玩家,我希望他们每个游戏发布小盲(sb)(y金额)和大盲(bb)(2y金额)(周期)。现在玩家每轮都会轮换。我的意思是:

Example in 8 player game: 
round1: sb,bb,3,4,5,6,7,8
round2: 1,sb,bb,4,5,6,7,8
round3: 1,2,sb,bb,5,6,7,8
.........................
round8: bb,2,3,4,5,6,7,sb
.........................
Example in 2 player game:
round1: sb,bb
round2: bb,sb
round3: sb,bb
.........................

我的代码:

stopNext = True
while check < 10:
    newDeck = Deck()
    [player.generateCard(newDeck) for player in players] # generate cards for each player
    a, b = itertools.cycle(players), itertools.cycle(players)
    if stopNext:
        next(b, None)
        stopNext = False
    one = a.next()
    two = b.next()
    one.postSB(one.amount, sb) # for each cycle select next instance
    two.postBB(two.amount, bb) # for each cycle select next instance starting at instance

我得到的2名球员和sb = 1,bb = 2是:

Next round
99 instance : <__main__.Player instance at 0x7f391cce0950>
98 instance : <__main__.Player instance at 0x7f391cce0998>
Next round
96 instance : <__main__.Player instance at 0x7f391cce0950>
96 instance : <__main__.Player instance at 0x7f391cce0950>
Next round
93 instance : <__main__.Player instance at 0x7f391cce0950>
93 instance : <__main__.Player instance at 0x7f391cce0950>
Next round
90 instance : <__main__.Player instance at 0x7f391cce0950>
90 instance : <__main__.Player instance at 0x7f391cce0950>
Next round
87 instance : <__main__.Player instance at 0x7f391cce0950>
87 instance : <__main__.Player instance at 0x7f391cce0950>

但我应该得到:

Next round
99
98
Next round
97
97
Next round
96
95
Next round
94
94
Next round
93
92

我认为使用next()的方式存在问题,正如您所看到的,从第3个开始,实例的地址始终相同,这意味着它是相同的实例。

2 个答案:

答案 0 :(得分:3)

您想要结合两种不同的食谱。

我们可以将这些结合起来。 pairwise通过从原始迭代器中生成两个新迭代器来工作 - 您需要这样做,以便在一个上调用next不会影响另一个。然后它将其中一个进入一个条目,izip将它们放在一起。我们在原始迭代器的cycle d版本上调用它,它将尾部连接到头部,为我们提供了无限版本。

In [6]:  import itertools as itt
         def pairwise(iterable):
             "s -> (s0,s1), (s1,s2), (s2, s3), ..."
             a, b = itt.tee(iterable)
             next(b, None)
             return itt.izip(a, b)

In [7]:  li = [1,2,3]
         pli = pairwise(itt.cycle(li))

In [8]:  pli.next()
Out[8]:  (1, 2)

In [9]:  pli.next()
Out[9]:  (2, 3)

In [10]: pli.next()
Out[10]: (3, 1)

In [11]: # or all at once:
         list(itt.islice(pli, 5))
Out[11]: [(1, 2), (2, 3), (3, 1), (1, 2), (2, 3)]

但是,在这种情况下,如果我们愿意,我们实际上可以避免使用tee,并将它们组合在一起

In[20]:  import itertools as itt
         def paircycle(iterable):
             "s -> (s0,s1), (s1,s2), (s2, s3), ... cycling back to the beginning of s"
             a, b = itt.cycle(iterable), itt.cycle(iterable)
             next(b, None)
             return itt.izip(a, b)

         li2 = [11, 12, 13]
         list(itt.islice(paircycle(li2),5))

Out[20]: [(11, 12), (12, 13), (13, 11), (11, 12), (12, 13)]

答案 1 :(得分:0)

我觉得你正在重新定义while循环的每次迭代的循环。尝试在while循环外定义g和h,然后在循环内调用.next行。然后从+1开始h,在进入循环之前调用.next一次。

相关问题