嵌套列表理解遇到意外错误

时间:2018-10-03 22:27:45

标签: python-3.x

我有这样的指示:

In [23]: d
Out[23]: 
{'string_formatting': ['__str__', '__repr__', '__format__', '__bytes__'],
 'rich_comparison': ['__gt__',
  '__ge__',
  '__lt__',
  '__le__',
  '__eq__',
  '__ne__'],
 'object_methods': ['__new__',
  '__init__',
  '__getattr__',
  '__setattr__',
  '__delattr__',
  '__getattribute__'],
 'class_service': ['__subclasshook__', '__init_subclass__']}

当我尝试理解列表时:

In [33]: [ i for i in value for value in d.values()]
Out[33]: 
['__subclasshook__',
 '__subclasshook__',
 '__subclasshook__',
 '__subclasshook__',
 '__init_subclass__',
 '__init_subclass__',
 '__init_subclass__',
 '__init_subclass__']

它没有按我预期的那样工作

[30]: arr = []

In [31]: for value in d.values():
    ...:     for i in value:
    ...:         arr.append(i)
    ...:         

In [32]: arr
Out[32]: 
['__str__',
 '__repr__',
 '__format__',
 '__bytes__',
 '__gt__',
 '__ge__',
 '__lt__',
 '__le__',
 '__eq__',
 '__ne__',...

我检查了几次,并确认我的理解没有问题。

我该如何解决问题?


0 个答案:

没有答案