在 python 中使用 list.remove(value_of_the_list) 抛出 ValueError
In python while using list.remove(value_of_the_list) a ValueError is thrown
通过 list.remove(value) 函数从类型列表的全局变量中清除一些值时,出现错误 "ValueError: list.remove(x): x not in list"
我的 class 定义如下所示:
class Deck(object):
global_l_init = ['2h','2d','2s','2c','3h','3d','3s','3c','4h','4d','4s','4c',
'5h','5d','5s','5c','6h','6d','6s','6c','7h','7d','7s','7c',
'8h','8d','8s','8c','9h','9d','9s','9c','10h','10d','10s','10c',
'Jh','Jd','Js','Jc','Qh','Qd','Qs','Qc','Kh','Kd','Ks','Kc',
'Ah','Ad','As','Ac']
#Cards' value
d={'2h':2,'2d':2,'2s':2,'2c':2,'3h':3,'3d':3,'3s':3,'3c':3,
'4h':4,'4d':4,'4s':4,'4c':4,'5h':5,'5d':5,'5s':5,'5c':5,
'6h':6,'6d':6,'6s':6,'6c':6,'7h':7,'7d':7,'7s':7,'7c':7,
'8h':8,'8d':8,'8s':8,'8c':8,'9h':9,'9d':9,'9s':9,'9c':9,
'10h':10,'10d':10,'10s':10,'10c':10,'Jh':10,'Jd':10,'Js':10,
'Jc':10,'Qh':10,'Qd':10,'Qs':10,'Qc':10,'Kh':10,'Kd':10,'Ks':10,'Kc':10,
'Ah':10,'Ad':10,'As':10,'Ac':10,'Ah':1,'Ad':1,'As':1,'Ac':1}
def __init__(self,plr_cur_value):
self.plr_cur_value = plr_cur_value
def deck_change(self):
self.global_l_init.remove(self.plr_cur_value)
我检查它是如何工作的,是:
D1=Deck(['2h','Jh'])
D1.deck_change()
错误如下:
ValueError Traceback (most recent call last)
<ipython-input-534-6f04c0ca0556> in <module>()
1 D1=Deck(['2h','Jh'])
----> 2 D1.deck_change()
<ipython-input-528-7691239e1d5b> in deck_change(self)
54
55 def deck_change(self):
---> 56 self.global_l_init.remove(self.plr_cur_value)
57
ValueError: list.remove(x): x not in list
我是 python 的新手。也许这个问题已经够愚蠢了。但仍然无法弄清楚为什么global_l_init
定义为class变量不是accessed/changed?...
感谢您的回答。
根据您提供的代码,您在 remove
期望的(要从 list
中删除的单个项目)和您提供的( list
个项目)。
您使用以下方法初始化您的实例:
D1=Deck(['2h','Jh'])
所以 self.plr_cur_value
是 ['2h','Jh']
,这意味着您的 remove
调用等同于:
self.global_l_init.remove(['2h','Jh'])
remove
查看你的 global_l_init
list
(其中只有长度 2 str
),试图查看它是否包含长度 2 list
的长度为 2 str
(它不包含任何 list
)。
如果目标是从 global_l_init
中删除每个元素,您需要更改:
self.global_l_init.remove(self.plr_cur_value)
至:
for card in self.plr_cur_value:
self.global_l_init.remove(card)
让它单独移除每张卡片。
请注意,当您需要重复执行成员资格测试 (O(n)
),或执行随机访问 list
插入或删除 (O(n)
每个)。由于您的所有值都是唯一的且不区分顺序,因此您最好在此处使用 set
,它具有 O(1)
插入、删除和成员资格测试:
class Deck(object):
#Cards' value
d={'2h':2,'2d':2,'2s':2,'2c':2,'3h':3,'3d':3,'3s':3,'3c':3,
'4h':4,'4d':4,'4s':4,'4c':4,'5h':5,'5d':5,'5s':5,'5c':5,
'6h':6,'6d':6,'6s':6,'6c':6,'7h':7,'7d':7,'7s':7,'7c':7,
'8h':8,'8d':8,'8s':8,'8c':8,'9h':9,'9d':9,'9s':9,'9c':9,
'10h':10,'10d':10,'10s':10,'10c':10,'Jh':10,'Jd':10,'Js':10,
'Jc':10,'Qh':10,'Qd':10,'Qs':10,'Qc':10,'Kh':10,'Kd':10,'Ks':10,'Kc':10,
'Ah':10,'Ad':10,'As':10,'Ac':10,'Ah':1,'Ad':1,'As':1,'Ac':1}
# Simplest way to make set here is to reuse d to make global_l_init w/o repeating yourself:
global_l_init = set(d)
def __init__(self,plr_cur_value):
self.plr_cur_value = set(plr_cur_value) # Convert to set on initialization
def deck_change(self):
# Optionally, make sure the cards are in the shared set before removing them
# to match list.remove behavior of raising ValueError when card not found
if not (self.plr_cur_value <= self.global_l_init):
raise ValueError("Cards in hand are not in the deck: {}".format(
', '.join(self.plr_cur_value - self.global_l_init)))
# Bulk remove entire hand now that we know it's available
self.global_l_init -= self.plr_cur_value
通过 list.remove(value) 函数从类型列表的全局变量中清除一些值时,出现错误 "ValueError: list.remove(x): x not in list"
我的 class 定义如下所示:
class Deck(object):
global_l_init = ['2h','2d','2s','2c','3h','3d','3s','3c','4h','4d','4s','4c',
'5h','5d','5s','5c','6h','6d','6s','6c','7h','7d','7s','7c',
'8h','8d','8s','8c','9h','9d','9s','9c','10h','10d','10s','10c',
'Jh','Jd','Js','Jc','Qh','Qd','Qs','Qc','Kh','Kd','Ks','Kc',
'Ah','Ad','As','Ac']
#Cards' value
d={'2h':2,'2d':2,'2s':2,'2c':2,'3h':3,'3d':3,'3s':3,'3c':3,
'4h':4,'4d':4,'4s':4,'4c':4,'5h':5,'5d':5,'5s':5,'5c':5,
'6h':6,'6d':6,'6s':6,'6c':6,'7h':7,'7d':7,'7s':7,'7c':7,
'8h':8,'8d':8,'8s':8,'8c':8,'9h':9,'9d':9,'9s':9,'9c':9,
'10h':10,'10d':10,'10s':10,'10c':10,'Jh':10,'Jd':10,'Js':10,
'Jc':10,'Qh':10,'Qd':10,'Qs':10,'Qc':10,'Kh':10,'Kd':10,'Ks':10,'Kc':10,
'Ah':10,'Ad':10,'As':10,'Ac':10,'Ah':1,'Ad':1,'As':1,'Ac':1}
def __init__(self,plr_cur_value):
self.plr_cur_value = plr_cur_value
def deck_change(self):
self.global_l_init.remove(self.plr_cur_value)
我检查它是如何工作的,是:
D1=Deck(['2h','Jh'])
D1.deck_change()
错误如下:
ValueError Traceback (most recent call last)
<ipython-input-534-6f04c0ca0556> in <module>()
1 D1=Deck(['2h','Jh'])
----> 2 D1.deck_change()
<ipython-input-528-7691239e1d5b> in deck_change(self)
54
55 def deck_change(self):
---> 56 self.global_l_init.remove(self.plr_cur_value)
57
ValueError: list.remove(x): x not in list
我是 python 的新手。也许这个问题已经够愚蠢了。但仍然无法弄清楚为什么global_l_init
定义为class变量不是accessed/changed?...
感谢您的回答。
根据您提供的代码,您在 remove
期望的(要从 list
中删除的单个项目)和您提供的( list
个项目)。
您使用以下方法初始化您的实例:
D1=Deck(['2h','Jh'])
所以 self.plr_cur_value
是 ['2h','Jh']
,这意味着您的 remove
调用等同于:
self.global_l_init.remove(['2h','Jh'])
remove
查看你的 global_l_init
list
(其中只有长度 2 str
),试图查看它是否包含长度 2 list
的长度为 2 str
(它不包含任何 list
)。
如果目标是从 global_l_init
中删除每个元素,您需要更改:
self.global_l_init.remove(self.plr_cur_value)
至:
for card in self.plr_cur_value:
self.global_l_init.remove(card)
让它单独移除每张卡片。
请注意,当您需要重复执行成员资格测试 (O(n)
),或执行随机访问 list
插入或删除 (O(n)
每个)。由于您的所有值都是唯一的且不区分顺序,因此您最好在此处使用 set
,它具有 O(1)
插入、删除和成员资格测试:
class Deck(object):
#Cards' value
d={'2h':2,'2d':2,'2s':2,'2c':2,'3h':3,'3d':3,'3s':3,'3c':3,
'4h':4,'4d':4,'4s':4,'4c':4,'5h':5,'5d':5,'5s':5,'5c':5,
'6h':6,'6d':6,'6s':6,'6c':6,'7h':7,'7d':7,'7s':7,'7c':7,
'8h':8,'8d':8,'8s':8,'8c':8,'9h':9,'9d':9,'9s':9,'9c':9,
'10h':10,'10d':10,'10s':10,'10c':10,'Jh':10,'Jd':10,'Js':10,
'Jc':10,'Qh':10,'Qd':10,'Qs':10,'Qc':10,'Kh':10,'Kd':10,'Ks':10,'Kc':10,
'Ah':10,'Ad':10,'As':10,'Ac':10,'Ah':1,'Ad':1,'As':1,'Ac':1}
# Simplest way to make set here is to reuse d to make global_l_init w/o repeating yourself:
global_l_init = set(d)
def __init__(self,plr_cur_value):
self.plr_cur_value = set(plr_cur_value) # Convert to set on initialization
def deck_change(self):
# Optionally, make sure the cards are in the shared set before removing them
# to match list.remove behavior of raising ValueError when card not found
if not (self.plr_cur_value <= self.global_l_init):
raise ValueError("Cards in hand are not in the deck: {}".format(
', '.join(self.plr_cur_value - self.global_l_init)))
# Bulk remove entire hand now that we know it's available
self.global_l_init -= self.plr_cur_value