从列表中删除多个随机项

时间:2017-03-05 05:26:27

标签: python

试图弄清楚如何从列表中删除多个随机项。这是我的代码。

playerdeck = random.sample(cardlist, 7)
print(playerdeck, "\n")
cardlist.remove(playerdeck[0,6])
print(cardlist)

虽然下面的内容实际上运行得很好,但我不确定如何在一个范围内完成。

cardlist.remove(playerdeck[0])

2 个答案:

答案 0 :(得分:1)

用列表理解来做。

playerdeck = random.sample(cardlist, 7)
print(playerdeck, "\n")
cardlist = [i for i in cardlist if i not in playerdeck]
print(cardlist)

答案 1 :(得分:0)

看起来你想要将牌组洗牌,然后将一些牌转移到玩家手中:

random.shuffle(cardlist)
playerdeck = cardlist[-7:]
cardlist[-7:] = []