GVKun编程网logo

python RuntimeError:字典在迭代过程中更改了大小(python中字典的修改)

21

关于pythonRuntimeError:字典在迭代过程中更改了大小和python中字典的修改的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Django:pythonmanage.pyr

关于python RuntimeError:字典在迭代过程中更改了大小python中字典的修改的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Django:python manage.py runserver提供了RuntimeError:在cmp中超过了最大递归深度、GCP上的Python3 Pytorch RuntimeError-无消息、matplotlib:RuntimeError:Python未作为框架安装、NameError:undefined – 在Ruby 2.1.2中更改了局部变量的解析规则吗?等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

python RuntimeError:字典在迭代过程中更改了大小(python中字典的修改)

python RuntimeError:字典在迭代过程中更改了大小(python中字典的修改)

我有这样的obj

{hello: ''world'', "foo.0.bar": v1, "foo.0.name": v2, "foo.1.bar": v3}

它应该扩展到

{ hello: ''world'', foo: [{''bar'': v1, ''name'': v2}, {bar: v3}]}

我在下面编写了代码,按拆分''.'',删除旧键,如果包含则追加新键''.'',但是它说RuntimeError: dictionary changedsize during iteration

def expand(obj):    for k in obj.keys():        expandField(obj, k, v)def expandField(obj, f, v):    parts = f.split(''.'')    if(len(parts) == 1):        return    del obj[f]    for i in xrange(0, len(parts) - 1):        f = parts[i]        currobj = obj.get(f)        if (currobj == None):            nextf = parts[i + 1]            currobj = obj[f] = re.match(r''\d+'', nextf) and [] or {}        obj = currobj    obj[len(parts) - 1] = v

对于obj.iteritems()中的k,v:

RuntimeError:词典在迭代过程中更改了大小

答案1

小编典典

就像消息说的那样:在循环遍历expand中的这些条目的过程中,您更改了expandField()内部obj中的条目数。

您可以尝试创建所需形式的新字典,或者以某种方式记录要进行的更改,然后在循环完成后进行更改。

Django:python manage.py runserver提供了RuntimeError:在cmp中超过了最大递归深度

Django:python manage.py runserver提供了RuntimeError:在cmp中超过了最大递归深度

我正在尝试从Django项目网站上的第1篇教程学习Django。我可能会缺少一些明显的东西,但是在遵循所有说明后,当我运行命令时

python manage.py runserver

我在请求的结尾处得到了错误消息,以寻求帮助(为简便起见,我仅将错误消息的重复行的前几行发布)。

这是我在网上找到的一些解决方案/建议,但对我没有帮助。

1)sys.setrecursionlimit(1500)。

这对我不起作用。

2)。DjangoRuntimeError:超过最大递归深度

这也不是一个选择,因为我没有使用PyDeV,我尝试使用pip卸载和安装Django,但它没有解决任何问题,并且我使用的是MountainLion的本机python,因为我们不建议将其卸载。

3)。我也尝试过:

 python manage.py runserver --settings=mysite.settings

与没有选项设置的命令完全相同的错误

任何建议,建议将不胜感激。我正在使用…。Django正式版。我使用pip和Python 2.7.2安装的1.5.1

Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10f7ee5d0>>Traceback (most recent call last):  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run    self.validate(display_num_errors=True)  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 280, in validate    num_errors = get_validation_errors(s, app)  File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors    for (app_name, error) in get_app_errors().items():  File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 166, in get_app_errors    self._populate()  File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 72, in _populate    self.load_app(app_name, True)  File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 96, in load_app    models = import_module(''.models'', app_name)  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module    __import__(name)  File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 370, in <module>    class AbstractUser(AbstractBaseUser, PermissionsMixin):  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 213, in __new__    new_class.add_to_class(field.name, copy.deepcopy(field))  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 265, in add_to_class    value.contribute_to_class(cls, name)  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 257, in contribute_to_class    cls._meta.add_field(self)  File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 179, in add_field    self.local_fields.insert(bisect(self.local_fields, field), field)  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>    ''__lt__'': [(''__gt__'', lambda self, other: other < self),  RuntimeError: maximum recursion depth exceeded in cmp

更新:所以我最终要做的是对安装virtualbox,在其上安装免费的ubuntu,然后继续完成本教程进行大肆宣传…哦!

答案1

小编典典

问题出在 functools.py 文件中。该文件来自Python。我刚刚安装了新版本的python 2.7.5,并且此文件是错误的(我还有另一个-
python 2.7.5的旧安装,并且那里的functools.py文件是正确的)

要解决此问题,请替换它(关于python \ Lib \ fuctools.py中的第56行):

convert = {    ''__lt__'': [(''__gt__'', lambda self, other: other < self),               (''__le__'', lambda self, other: not other < self),               (''__ge__'', lambda self, other: not self < other)],    ''__le__'': [(''__ge__'', lambda self, other: other <= self),               (''__lt__'', lambda self, other: not other <= self),               (''__gt__'', lambda self, other: not self <= other)],    ''__gt__'': [(''__lt__'', lambda self, other: other > self),               (''__ge__'', lambda self, other: not other > self),               (''__le__'', lambda self, other: not self > other)],    ''__ge__'': [(''__le__'', lambda self, other: other >= self),               (''__gt__'', lambda self, other: not other >= self),               (''__lt__'', lambda self, other: not self >= other)]}

对此:

convert = {    ''__lt__'': [(''__gt__'', lambda self, other: not (self < other or self == other)),               (''__le__'', lambda self, other: self < other or self == other),               (''__ge__'', lambda self, other: not self < other)],    ''__le__'': [(''__ge__'', lambda self, other: not self <= other or self == other),               (''__lt__'', lambda self, other: self <= other and not self == other),               (''__gt__'', lambda self, other: not self <= other)],    ''__gt__'': [(''__lt__'', lambda self, other: not (self > other or self == other)),               (''__ge__'', lambda self, other: self > other or self == other),               (''__le__'', lambda self, other: not self > other)],    ''__ge__'': [(''__le__'', lambda self, other: (not self >= other) or self == other),               (''__gt__'', lambda self, other: self >= other and not self == other),               (''__lt__'', lambda self, other: not self >= other)]}

另请阅读:http : //regebro.wordpress.com/2010/12/13/python-
implementing-rich-comparison-the-correct-
way/

GCP上的Python3 Pytorch RuntimeError-无消息

GCP上的Python3 Pytorch RuntimeError-无消息

Anthony Leo非常感谢您的详细回答! 不幸的是,这最终成为我在设置服务器时安装的其中一个模块的问题。
但这最终不是服务器本身或代码的问题,我只是在设置时错误地安装了模块。

对于其他人在这个问题上花费的所有时间,我感到很抱歉。

,

就找到获取有关该问题的更多信息的方式而言,以弄清楚为什么会发生此问题。您可以将故障排除分为两层:

  1. 应用层
  2. GCE VM实例层

在大多数情况下,我们将重点关注GCE VM实例层,因为在此位置可能会找到更多信息,因为这些日志将向我们显示GCE instacne在运行之前或之后出现问题的信息。您在上面介绍的stacktrace。

根据您的VM实例配置,建议将Cloud Logging Agent安装到受影响的VM上,以便我们可以从VM内部收集日志。这也将有所帮助,因为收集的这些日志是准确的。

一旦在VM上安装并运行了代理,我们就可以将自己定向到GCP上的Logs Explorer控制台,这将使我们能够从上面提到的层中查看两种类型的日志。请记住,通过此步骤,您应该重新运行您的应用程序及其方案。

从此处开始,我们可以使用Logs Explorer在logs queries中查看所有日志并根据时间戳,资源类型等对它们进行排序。这将是一个很好的起点,因为它将允许您按照导致错误的日志的时间顺序查看所有日志。这样一来,您就可以找出发生此问题的原因和/或提供解决此问题的线索。

matplotlib:RuntimeError:Python未作为框架安装

matplotlib:RuntimeError:Python未作为框架安装

这个问题以前在这里,也在这里被问过。但是,该解决方案无法解决我的问题。

原始错误是,当我尝试时import matplotlib.pyplot,我得到:

追溯(最近一次通话):文件“ /”,“用户/XX/anaconda/lib/python2.7/site-
packages/matplotlib/pyplot.py”中的行1,位于_backend_mod,new_figure_manager,
draw_if_interactive,_show = pylab_setup()文件“
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/
init.py“,第32行,位于pylab_setup globals(),locals(),[backend_name],0)文件“
/Users/XX/anaconda/lib/python2.7/site-
packages/matplotlib/backends/backend_macosx.py”
matplotlib.backends中的第24行输入import _macosx
RuntimeError:Python未作为框架安装。如果未将Python作为框架安装,则Mac OS
X后端将无法正常运行。有关更多信息,请参见Python文档。在Mac OS
X上安装Python作为框架。请重新安装Python作为框架,或尝试其他后端之一。如果您在虚拟环境中使用Matplotlib,请参阅Matplotlib常见问题解答中的“在虚拟环境中使用Matplotlib”

我跟着解决方案添加一个~/.matplotlib/matplotlibrc文件的代码:backend: TkAgg。之后,我的错误更改为:

/Users/XX/anaconda/lib/python2.7/site-
packages/matplotlib/font_manager.py:273:UserWarning:Matplotlib正在使用fc-
list构建字体缓存。这可能需要一点时间。warnings.warn(“ Matplotlib正在使用fc-
list构建字体缓存。这可能需要一点时间。”)objc
[25120]:TKApplication类在/Users/XX/anaconda/lib/libtk8.5.dylib和/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk。将使用两者之一。哪一个未定义。objc
[25120]:TKMenu类在/Users/XX/anaconda/lib/libtk8.5.dylib和/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk中都实现。将使用两者之一。哪一个未定义。objc
[25120]:TKContentView类在/Users/XX/anaconda/lib/libtk8.5.dylib和/System/Library/Frameworks/Tk.framework/Versions/8中实现。5
/ Tk。将使用两者之一。哪一个未定义。objc
[25120]:在/Users/XX/anaconda/lib/libtk8.5.dylib和/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk中都实现了TKWindow类。将使用两者之一。哪一个未定义。

我不知道该如何解决。我没有使用虚拟机。你可以帮帮我吗?谢谢!

PS:我发现通过添加:

导入matplotlib
matplotlib.use(’TkAgg’)

之前import matplotlib.pyplot,它似乎可以工作。但是每次都添加这两行代码很烦人……有人知道发生了什么,我如何解决?谢谢!

NameError:undefined – 在Ruby 2.1.2中更改了局部变量的解析规则吗?

NameError:undefined – 在Ruby 2.1.2中更改了局部变量的解析规则吗?

我得到NameError:未定义的局部变量或使用 ruby 2.1.2的方法

正如在this question中观察到的那样,表达式如:

bar if bar = true

引发未定义的局部变量错误(假设事先未定义bar),因为解析器在分配之前会读取bar.而且我相信这个表达方式与以前没什么区别:

bar if bar = false

两者之间的区别在于主体是否被评估,但是如果遇到未定义的局部变量,则在评估条件之前立即引发错误并不重要.

但是当我在Ruby 2.1.2上运行第二个代码时,它不会引发错误.它之前是这样吗?如果是这样,那么解析讨论的内容是什么?如果没有,Ruby规范是否已更改?有没有提到这个?它在1.8.7,1.9.3等中做了什么?

解决方法

是的,它在ruby2.1.2中有所改变

在1.8.7,1.9.3,2.0.0甚至2.1.1中,我得到2个警告,没有错误:

2.0.0-p247 :007 > bar if bar = false
(irb):7: warning: found = in conditional,should be ==
 => nil 
2.0.0-p247 :008 > bar if bar = true
(irb):8: warning: found = in conditional,should be ==
 => true

而在2.1.2版本中你提到我得到2个警告和1个NameError错误.

2.1.2 :001 > bar if bar = true
(irb):1: warning: found = in conditional,should be ==
NameError: undefined local variable or method `bar' for main:Object
        from (irb):1
        from /home/durrantm/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > bar if bar = false
(irb):2: warning: found = in conditional,should be ==
 => nil

这是在我的Ubuntu 14上

我们今天的关于python RuntimeError:字典在迭代过程中更改了大小python中字典的修改的分享已经告一段落,感谢您的关注,如果您想了解更多关于Django:python manage.py runserver提供了RuntimeError:在cmp中超过了最大递归深度、GCP上的Python3 Pytorch RuntimeError-无消息、matplotlib:RuntimeError:Python未作为框架安装、NameError:undefined – 在Ruby 2.1.2中更改了局部变量的解析规则吗?的相关信息,请在本站查询。

本文标签: