POKERHANDS - 如何在使用后从列表中删除
POKERHANDS - How to I delete from list after the use
我有那个代码。如果我输入 n 个参数是 3,它将是 3 手。
但我只想使用任何一张卡一次。
总共有52张牌。每张卡只能使用一次。
使用后如何删除卡片?
顺便说一下,stdio.writeln 就像打印一样。一样。
n = int(sys.argv[1])
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
"King", "Ace"]
rank = random.randrange(0, len(RANKS))
suit = random.randrange(0, len(SUITS))
deck = []
for rank in RANKS:
for suit in SUITS:
card = rank + " of " + suit
deck += [card]
for i in range(n):
a = len (deck)
for i in range(a):
r = random.randrange(i, a)
temp = deck[r]
deck[r] = deck[i]
deck[i] = temp
for i in range(5):
stdio.writeln(deck[i] + " ")
stdio.writeln()
利用列表理解来组成一整堆卡片,
使用 random.sample() and draw cards up by list.pop() 从中获取混合卡的副本,这将自动删除卡。
我减少了一点 RANKS
来缩短 print-outs:
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6"] #,'7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
import random
# in order
StackOfCards = [ f'{r} of {s}' for r in RANKS for s in SUITS]
# return random.sample() of full length input == random shuffled all cards
mixedDeck = random.sample(StackOfCards, k = len(StackOfCards))
print("Orig:",StackOfCards)
print()
print("Mixed:",mixedDeck)
print()
# draw 6 cards into "myHand" removing them from "mixedDeck":
myHand = [mixedDeck.pop() for _ in range(6)]
print("Hand:",myHand)
print()
print("Mixed:",mixedDeck)
输出:
Orig: ['2 of Clubs', '2 of Diamonds', '2 of Hearts', '2 of Spades', '3 of Clubs',
'3 of Diamonds', '3 of Hearts', '3 of Spades', '4 of Clubs', '4 of Diamonds',
'4 of Hearts', '4 of Spades', '5 of Clubs', '5 of Diamonds', '5 of Hearts',
'5 of Spades', '6 of Clubs', '6 of Diamonds', '6 of Hearts', '6 of Spades']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades', '6 of Diamonds',
'2 of Diamonds', '3 of Clubs', '2 of Hearts', '5 of Diamonds', '4 of Hearts']
Hand: ['4 of Hearts', '5 of Diamonds', '2 of Hearts',
'3 of Clubs', '2 of Diamonds', '6 of Diamonds']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades']
使用 .pop()
减轻了删除某些内容的需要,如果您真的不喜欢它(为什么会这样?),您可以通过以下方式重新创建一个没有另一个列表元素的列表:
l = [1,2,3,4,5,6]
p = [2,6]
l[:] = [ x for x in l if x not in p] # inplace modifies l to [1,3,4,5]
我有那个代码。如果我输入 n 个参数是 3,它将是 3 手。 但我只想使用任何一张卡一次。 总共有52张牌。每张卡只能使用一次。 使用后如何删除卡片?
顺便说一下,stdio.writeln 就像打印一样。一样。
n = int(sys.argv[1])
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
"King", "Ace"]
rank = random.randrange(0, len(RANKS))
suit = random.randrange(0, len(SUITS))
deck = []
for rank in RANKS:
for suit in SUITS:
card = rank + " of " + suit
deck += [card]
for i in range(n):
a = len (deck)
for i in range(a):
r = random.randrange(i, a)
temp = deck[r]
deck[r] = deck[i]
deck[i] = temp
for i in range(5):
stdio.writeln(deck[i] + " ")
stdio.writeln()
利用列表理解来组成一整堆卡片, 使用 random.sample() and draw cards up by list.pop() 从中获取混合卡的副本,这将自动删除卡。
我减少了一点 RANKS
来缩短 print-outs:
SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"]
RANKS = ["2", "3", "4", "5", "6"] #,'7", "8", "9", "10", "Jack", "Queen", "King", "Ace"]
import random
# in order
StackOfCards = [ f'{r} of {s}' for r in RANKS for s in SUITS]
# return random.sample() of full length input == random shuffled all cards
mixedDeck = random.sample(StackOfCards, k = len(StackOfCards))
print("Orig:",StackOfCards)
print()
print("Mixed:",mixedDeck)
print()
# draw 6 cards into "myHand" removing them from "mixedDeck":
myHand = [mixedDeck.pop() for _ in range(6)]
print("Hand:",myHand)
print()
print("Mixed:",mixedDeck)
输出:
Orig: ['2 of Clubs', '2 of Diamonds', '2 of Hearts', '2 of Spades', '3 of Clubs',
'3 of Diamonds', '3 of Hearts', '3 of Spades', '4 of Clubs', '4 of Diamonds',
'4 of Hearts', '4 of Spades', '5 of Clubs', '5 of Diamonds', '5 of Hearts',
'5 of Spades', '6 of Clubs', '6 of Diamonds', '6 of Hearts', '6 of Spades']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades', '6 of Diamonds',
'2 of Diamonds', '3 of Clubs', '2 of Hearts', '5 of Diamonds', '4 of Hearts']
Hand: ['4 of Hearts', '5 of Diamonds', '2 of Hearts',
'3 of Clubs', '2 of Diamonds', '6 of Diamonds']
Mixed: ['2 of Clubs', '3 of Diamonds', '6 of Hearts', '4 of Spades', '5 of Clubs',
'3 of Hearts', '6 of Spades', '5 of Hearts', '4 of Diamonds', '3 of Spades',
'2 of Spades', '6 of Clubs', '4 of Clubs', '5 of Spades']
使用 .pop()
减轻了删除某些内容的需要,如果您真的不喜欢它(为什么会这样?),您可以通过以下方式重新创建一个没有另一个列表元素的列表:
l = [1,2,3,4,5,6]
p = [2,6]
l[:] = [ x for x in l if x not in p] # inplace modifies l to [1,3,4,5]