list.reverse 原地反转并返回 None,所以
for items in some_2d_list:
items = items.reverse()
反转仍在some_2d_list 中的现有列表并将None 分配给items。
当您在for items in some_2d_list 中输入代码块时,items 是对仍在some_2d_list 中的对象的引用。任何修改现有列表的内容也会影响some_2d_list。例如
>>> some_2d_list = [['mean', 'really', 'is', 'jean'],
... ['world', 'my', 'rocks', 'python']]
>>>
>>> for items in some_2d_list:
... items.append('foo')
... del items[1]
...
>>> some_2d_list
[['mean', 'is', 'jean', 'foo'], ['world', 'rocks', 'python', 'foo']]
像“+=”这样的增强操作是模棱两可的。根据任何给定类型的实现方式,它可以就地更新或创建新对象。他们为列表工作
>>> some_2d_list = [['mean', 'really', 'is', 'jean'],
... ['world', 'my', 'rocks', 'python']]
>>>
>>> for items in some_2d_list:
... items += ['bar']
...
>>> some_2d_list
[['mean', 'really', 'is', 'jean', 'bar'], ['world', 'my', 'rocks', 'python', 'bar']]
但不适用于元组
>>> some_2d_list = [('mean', 'really', 'is', 'jean'), ('world', 'my', 'rocks', 'python')]
>>> for items in some_2d_list:
... items += ('baz',)
...
>>> some_2d_list
[('mean', 'really', 'is', 'jean'), ('world', 'my', 'rocks', 'python')]