English 中文(简体)
为什么我不能在联络处开会时删除名单内容?
原标题:Why can I not delete list elements while iterating in an OO setting?

My situation is as follows: I m working on an implementation of BlackJack. I ve got classes for Card, Hand, Player, Deck, and Game. The main game object stores players and a deck, while players stores hands which store cards.

我常常做如下事情。 在这个例子中,我向每个参与者发放了最初的卡片。

num_cards = 2
for player in self.players:
    new_hand = Hand()
    for i in range(num_cards):
        new_hand.add_card(self.deck.get_next_card())
    player.assign_hand(new_hand)

这一工作很简单。 我现在的问题是,我希望删除一组参与者的一手手(一个角色可以在黑杰克分裂,造成一手以上。 在以下职能中,我打算通过每个参与者的手。 如果手面价值大于21,我想删除手头。 (请注意,下文所述移除功能通常在运动员类别中进行,采用代号为正文的运动员方法。) 我也存在同样的问题,因此,我已将该守则移至某些地方,以备保存。

for player in self.players:
    for hand in player.hands:
        if hand.smallest_value() > 21:
            player.hands.remove(hand)

这并不可行。 显而易见,我能够在拆除线前印制手稿,之后不印制。 这就是说,这似乎已被删除。 然而,在接下来的游戏中,这又是一回头。 因此,参与者的双向发展。

以上法典的职能是确认“游戏者”在游戏中。 这一功能是从一个称为游戏的卷宗中发出的,该档案是为了开始/结束游戏,并为主要的游戏圈提供便利。 因此,唯一要求验证_互动器_手(手)的人正在发挥作用。 py file, one indent in, in themmer. 我呼吁:

game.validate_player_hands()

我也尝试利用关键词确定手法指数,但结果相同。

为什么清单要素(一个称为参与者清单中的物体)在删除时不会删除?

提前感谢,

ParagonRG

最佳回答

制作一份与物体长度有关的索引的复制件并对该物体进行检索。 列出每个内容,以零件删除,然后过滤手,以净化零件。

for player in self.players:
    for hand_idx in range(len(player.hands)):
        if player.hands[hand_idx].smallest_value() > 21:
            player.hands[hand_idx]=0
    player.hands=filter(None,hands)
问题回答

如何使用简单的清单来消除手法:

for player in self.players:
    player.hands = [hand for hand in player.hands if hand.smallest_value() <= 21]

http://www.ohchr.org。

过滤器:

for player in self.players:
    player.hands = filter(lambda x: x.smallest_value() <= 21, player.hands)

你们想要做的是:

newList = hands[:]   
for hand in newList:  
    if hand.smallest_value() > 21:  #your code here
            player.hands.remove(hand)

这样,你就可以在翻阅这份清单时修改一份清单,从而避免了Mihai提到的“树木气候”情景。

可以通过在“for”声明的末尾添加[:]加以解决。 这份清单的复印件。 然后,在修改原始清单时,你可以转过复印件:

for player in self.players:
    for hand in player.hands[:]:
        if hand.smallest_value() > 21:
            player.hands.remove(hand)

http://docs.python.org/tutorial/introduction.html#lists”rel=“nofollow noreferer”>list slicing syntax。 https://stackoverflow.com/a/2690/37386” 复制名单的方式。 更经常地以<编码>t_list[3:8]





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签