1

タイトルが示すように、リストからランダムなアイテムを削除するにはどうすればよいですか? 私はテキストベースのゲームを作成しています。以下に示すように、アイテムをランダムに取得してリストから削除するリストがあります。

Deck = ['Lumina, Lighsworn Summoner', 'Lumina, Lighsworn Summoner', 'Judgment Dragon', 'Judgment Dragon', 'Judgment Dragon', 'Jain, Lightsworn Paladin', 'Ehren, Lightsworn Monk', 'Lyla, Lightsworn Sorceress', 'Lyla, Lightsworn Sorceress', 'Ryko, Lighsworn Hunter', 'Ryko, Lighsworn Hunter', 'Ryko, Lighsworn Hunter', 'Celestia, Lightsworn Angel', 'Aurkus, Lightsworn Druid', 'Garoth, Lightsworn Warrior', 'Garoth, Lightsworn Warrior', 'Lightray Gearfried', 'Lightray Gearfried', 'Lightray Gearfried', 'Lightray Daedalus', 'Lightray Daedalus', 'Lightray Daedalus', 'Lightray Diabolos', 'Lightray Diabolos', 'Lightray Diabolos', 'Sephylon, the Ultimate Timelord', 'Sephylon, the Ultimate Timelord', 'Sephylon, the Ultimate Timelord', 'Card Trooper', 'Card Trooper', 'Honest', 'Gorz the Emissary of Darkness', 'Necro Gardna', 'Necro Gardna', 'Necro Gardna', 'Charge of the Light Brigade', 'Solar Recharge', 'Solar Recharge', 'Solar Recharge', 'Beckoning Light', 'Beckoning Light']
loop = 1
while loop == 1:
    option = raw_input()
    if option == 'draw':
        newcard = random.sample(Deck, 1)
        print newcard
        Deck.remove(newcard)

ただし、ゲーム内の「コマンド」「描画」を試みると、常に次の出力とリスト関連のエラーが発生します。

draw
['Judgment Dragon']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "YGOGame.py", line 183, in <module>
    Deck.remove(newcard)
ValueError: list.remove(x): x not in list

どんな助けでも大歓迎です。

4

1 に答える 1

7

newcardリストです(リストrandom.sample(Deck, 1)を返すを使用しました)。使用する:

Deck.remove(newcard[0])

または を使用して1 つの要素random.choice()を選択します。

newcard = random.choice(Deck)
于 2012-12-28T21:31:46.233 に答える