gpt4 book ai didi

python - 我收到 RuntimeError : dictionary changed size during iteration. Python

转载 作者:行者123 更新时间:2023-11-28 21:16:16 27 4
gpt4 key购买 nike

这是我的示例代码:

def clean(record):

record.pop('configurable_fields')
record.pop('applied_discounts')
record.pop('product_options')

for key, value in record.items():
if value is None:
record.pop(key)

return record

我收到这个错误:

Traceback (most recent call last):
File "C:\Users\Filipe\Dropbox\code\bigAnalysis\finalized scripts\importer.py", line 76, in <module>
process_file('allOrderProductsData')
File "C:\Users\Filipe\Dropbox\code\bigAnalysis\finalized scripts\importer.py", line 73, in process_file
process_line(line)
File "C:\Users\Filipe\Dropbox\code\bigAnalysis\finalized scripts\importer.py", line 58, in process_line
product_id = dump_record('package', clean(package)) # id of recently created record
File "C:\Users\Filipe\Dropbox\code\bigAnalysis\finalized scripts\importer.py", line 40, in clean
for key, value in record.items():
RuntimeError: dictionary changed size during iteration

我尝试了一些在其他线程中找到的解决方案,例如使用 copy.deecopy,但目前没有一个有效。

最佳答案

使用

for key, value in list(record.items()):

代替

for key, value in record.items():

这样,您就可以“卡住”要迭代的项目列表。 record.items() 是一个仍然持有对原始字典的引用的生成器。 list(record.items()) 是一个独立的列表。

关于python - 我收到 RuntimeError : dictionary changed size during iteration. Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28956968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com