Python 如何从列表中的列表中删除某些int?

Python 如何从列表中的列表中删除某些int?,python,list,function,random,blackjack,Python,List,Function,Random,Blackjack,我正在尝试做一个21点游戏作为一个初学者项目。当我试图从牌组中移除正在处理的牌时,我得到以下信息:ValueError:list。移除(x):x不在列表中。我怎样才能解决这个问题 这是我的代码: import random deck = [[2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7],

我正在尝试做一个21点游戏作为一个初学者项目。当我试图从牌组中移除正在处理的牌时,我得到以下信息:ValueError:list。移除(x):x不在列表中。我怎样才能解决这个问题

这是我的代码:

import random

deck = [[2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4],
        [5, 5, 5, 5],
        [6, 6, 6, 6],
        [7, 7, 7, 7],
        [8, 8, 8, 8],
        [9, 9, 9, 9],
        [10, 10, 10, 10],
        [10, 10, 10, 10],
        [10, 10, 10, 10],
        [10, 10, 10, 10],
        [11, 11, 11, 11]
        ]

def deal_cards():
    number = random.choice(deck[0:][0:]) # selecting the number of the card
    card = random.choice(number) # selecting wich suit from the number sould be the card
    new_deck = deck.remove(card) # Here is the problem
    print(new_deck)
    print(card)

deal_cards()

嵌套列表的行为类似于列表的列表。这意味着您必须指定第一个列表的索引才能访问嵌套列表中的项目

new_deck = deck[foo].remove(card)


这进入列表[foo],例如,让foo=1。列表应该是[3,3,3,3]。

卡是一个int-not列表。这就是为什么会出现此错误。你的牌组包含一个列表。如果要删除单个int,则应指定应删除的列表。 您应按以下方式更改代码:

def deal_cards():
    number = random.choice(deck[0:][0:])# selecting the number of the card
    card = random.choice(number)# selecting wich suit from the number
    deck[deck.index(number)].remove(card) # problem fixed
    print(deck) # remove returns nothing
    print(card)