python throws_Python 2.7 throws ValueError: list.remove(x): x not in list

本文介绍了在Python中处理列表时的一些常见陷阱,特别是在循环中删除列表元素时的问题。为了避免这些问题,文章推荐使用列表副本进行操作,并提供了一段示例代码说明如何在遍历列表的同时安全地移除元素。此外,还展示了一个更复杂的例子,涉及两个列表间的碰撞检测和健康值更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You should not remove items from a list you are looping over. Create a copy instead: for a in aliens[:]:

and for b in bolts[:]:

Modifying a list while looping over it, affects the loop: >>> lst = [1, 2, 3] >>> for i in lst: ... print i ... lst.remove(i) ... 1 3 >>> lst [2]

Removing items from a list you are looping over twice makes things a little more complicated still, resulting in a ValueError:

When creating a copy of the lists you are modifying at each level of your loops, you avoid the problem: >>> lst = [1, 2, 3] >>> for i in lst[:]: ... for i in lst[:]: ... print i, lst ... lst.remove(i) ... 1 [1, 2, 3] 2 [2, 3] 3 [3]

When you have a collision, you only need to remove the b bolt once, not in the loop where you hurt the aliens. Clean out the aliens separately later: def manage_collide(bolts, aliens): for b in bolts[:]: for a in aliens: if b['rect'].colliderect(a['rect']) and a['health'] > 0: bolts.remove(b) for a in aliens: a['health'] -= 1 for a in aliens[:]: if a['health']

E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert.py:854: UserWarning: Statistics for quantized inputs were expected, but not specified; continuing anyway. warnings.warn( WARNING: All log messages before absl::InitializeLog() is called are written to STDERR W0000 00:00:1750592469.236287 1300 tf_tfl_flatbuffer_helpers.cc:365] Ignored output_format. W0000 00:00:1750592469.236390 1300 tf_tfl_flatbuffer_helpers.cc:368] Ignored drop_control_dependency. 2025-06-22 19:41:09.237337: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 2025-06-22 19:41:09.249839: I tensorflow/cc/saved_model/reader.cc:52] Reading meta graph with tags { serve } 2025-06-22 19:41:09.249859: I tensorflow/cc/saved_model/reader.cc:147] Reading SavedModel debug info (if present) from: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 I0000 00:00:1750592469.381787 1300 mlir_graph_optimization_pass.cc:425] MLIR V1 optimization pass is not enabled 2025-06-22 19:41:09.404910: I tensorflow/cc/saved_model/loader.cc:236] Restoring SavedModel bundle. 2025-06-22 19:41:10.235403: I tensorflow/cc/saved_model/loader.cc:220] Running initialization op on SavedModel bundle at path: C:\Users\27731\AppData\Local\Temp\tmpou2lior2 2025-06-22 19:41:10.478873: I tensorflow/cc/saved_model/loader.cc:471] SavedModel load for tags { serve }; Status: success: OK. Took 1241546 microseconds. 2025-06-22 19:41:10.657810: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable. Traceback (most recent call last): File "E:\pycharm\study\计算机视觉\test2.py", line 302, in <module> deployer.convert_to_tflite_with_metadata(tflite_path, quantize=True) File "E:\pycharm\study\计算机视觉\test2.py", line 255, in convert_to_tflite_with_metadata tflite_model = converter.convert() File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1250, in wrapper return self._convert_and_export_metrics(convert_func, *args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1202, in _convert_and_export_metrics result = convert_func(self, *args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1768, in convert saved_model_convert_result = self._convert_as_saved_model() File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1749, in _convert_as_saved_model return super(TFLiteKerasModelConverterV2, self).convert( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1494, in convert return self._optimize_tflite_model( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 215, in wrapper raise error from None # Re-throws the exception. File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 205, in wrapper return func(*args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 1146, in _optimize_tflite_model model = self._quantize( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\lite.py", line 760, in _quantize calibrated = calibrate_quantize.calibrate( File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 215, in wrapper raise error from None # Re-throws the exception. File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\convert_phase.py", line 205, in wrapper return func(*args, **kwargs) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\optimize\calibrator.py", line 258, in calibrate self._feed_tensors(dataset_gen, resize_input=True) File "E:\python3.9.13\lib\site-packages\tensorflow\lite\python\optimize\calibrator.py", line 152, in _feed_tensors self._calibrator.FeedTensor(input_array) ValueError: Cannot set tensor: Got value of type UINT8 but expected type FLOAT32 for input 0, name: serving_default_input_layer_1:0
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值