[英]Removing more than one random item from a list
Trying to figure out how to remove more than one random item from a list. 试图弄清楚如何从列表中删除多个随机项目。 Here's the code i have.
这是我的代码。
playerdeck = random.sample(cardlist, 7)
print(playerdeck, "\n")
cardlist.remove(playerdeck[0,6])
print(cardlist)
Although this below actually works just fine, I was not sure how to do it in a range. 尽管下面的代码实际上可以正常工作,但是我不确定如何在一定范围内做到这一点。
cardlist.remove(playerdeck[0])
Do it with a list comprehension. 用列表理解来做。
playerdeck = random.sample(cardlist, 7)
print(playerdeck, "\n")
cardlist = [i for i in cardlist if i not in playerdeck]
print(cardlist)
It looks like you want to shuffle a deck and then transfer some cards to the player's hand: 您似乎想洗牌,然后将一些牌移到玩家的手上:
random.shuffle(cardlist)
playerdeck = cardlist[-7:]
cardlist[-7:] = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.