[英]How do I delete a list within a list in Python?
My program involves drawing a card from a deck, adding the value of it to my hand and then deleting the card from the deck (since I don't want to draw the same card twice). 我的程序涉及从卡组中抽取一张卡,将其值添加到我的手上,然后从卡组中删除该卡(因为我不想两次绘制同一张卡)。
Example of a card: 卡示例:
card = ["4 of clubs", 4]
The code that doesn't work: 无效的代码:
card_number = random.randrange(len(deck_cards))
card = deck_cards[card_number][0]
print(card)
hand_value += deck_cards[card_number][1]
deck_cards.remove(card_number)
I have no idea what I am doing wrong, and if I change the code to 我不知道我在做什么错,如果我将代码更改为
card_number = random.chioce(deck_cards)
it won't accept the card = deck_cards[card_number][0]
part. 它将不接受card = deck_cards[card_number][0]
部分。
What to do? 该怎么办?
list.remove
takes an object, not an index, as parameter. list.remove
将对象而不是索引作为参数。
So you should modify your code to retrieve a card and use it directly: 因此,您应该修改代码以检索卡并直接使用它:
>>> card = random.choice(deck_cards) # Retrieve an object, not an index
>>> print (card) # card is you card list, i.e.:
["4 of clubs", 4]
>>> print (card[0])
4 of clubs
>>> hand_value += card[1]
>>> deck_cards.remove (card)
Note that you can also delete an object at a specified index using the del
built-in: 请注意,您还可以使用内置的del
指定索引处的对象:
>>> card_number = random.randrange(len(deck_cards))
>>> card = deck_cards[card_number][0]
>>> print(card)
"4 of clubs"
>>> hand_value += deck_cards[card_number][1]
>>> del deck_cards[card_number]
random.choice(list)
returns a random element of the list, but the list deck_cards[card_number]
expects the position of the item to retrieve from the list (hence an integer). random.choice(list)
返回random.choice(list)
的随机元素,但是列表deck_cards[card_number]
希望从列表中检索项目的位置(因此为整数)。 So just stating card = random.choice(deck_cards)[0]
is sufficient. 因此,只需说明card = random.choice(deck_cards)[0]
就足够了。
Also, using a list as reprensentation of a card is a poor choice, because it does not represent the object characteristic of a card. 同样,使用列表作为卡的代表不是一个好选择,因为它不代表卡的对象特征。 If you wrote you own class for cards, your code would become much more readable. 如果您为卡编写了自己的类,则代码将更具可读性。 If the class would look like this: 如果该类如下所示:
class Card(object):
def __init__(self, name, value):
self.name = name
self.value = value
and you instantiated a card like card = Card('4 of clubs', 4)
, you could write code like hand_value = deck_cards[index].value
and get rid of those ugly indices. 然后实例化一张卡片,例如card = Card('4 of clubs', 4)
,则可以编写类似hand_value = deck_cards[index].value
并摆脱那些难看的索引。 Of curse you would have to write you own string presenter for this class for easy fancy output of a card, but that's not to hard either. 当然,您必须为此类编写自己的字符串演示器,以轻松实现卡的精美输出,但这也不难。
So (without that string representational code) you problem would be solved like this: 因此(没有该字符串表示代码),您的问题将像这样解决:
card = random.choice(deck_cards)
print('{},{}'.format(card.name, card.value))
hand_value += card.value
deck_cards.remove(card)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.