代码
有问题的代码如下:
# 未找到资源的keys 集合
unFinded = set(self.getJsonAnalysis(jsonPath))
str_pattern = re.compile(r".*[0-9]$")
for unfind in unFinded:
if not str_pattern.match(unfind) and not unfind.endswith('empty') and not unfind.endswith('Default'):
unFinded.remove(unfind)
print("remove :{}".format(unfind))
return unFinded
解决问题的代码:copy()
# 未找到资源的keys 集合
unFinded = set(self.getJsonAnalysis(jsonPath))
str_pattern = re.compile(r".*[0-9]$")
for unfind in unFinded.copy():
if not str_pattern.match(unfind) and not unfind.endswith('empty') and not unfind.endswith('Default'):
unFinded.remove(unfind)
print("remove :{}".format(unfind))
return unFinded
本文介绍了一段Python代码中出现的运行时错误“Set changed size during iteration”,该问题发生在尝试从集合中移除元素的同时进行迭代的过程中。通过使用集合的copy方法,解决了在迭代过程中修改集合的问题。

2123

被折叠的 条评论
为什么被折叠?



