简体   繁体   中英

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:] = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM