以下内容将“就地”反转,并在Python 2和3中工作:
>>> mylist = [1, 2, 3, 4, 5]
>>> mylist[:] = reversed(mylist)
>>> mylist
[5, 4, 3, 2, 1]
为什么/怎么样?以来 reversed
给我一个迭代器,并不事先复制列表,从那以后 [:]=
取代“就地”,我很惊讶。以下,也使用 reversed
,按预期打破:
>>> mylist = [1, 2, 3, 4, 5]
>>> for i, item in enumerate(reversed(mylist)):
mylist[i] = item
>>> mylist
[5, 4, 3, 4, 5]
为什么不呢 [:] =
那样的失败?
是的,我知道 mylist.reverse()
。
CPython list slice assigment将首先通过调用将iterable转换为列表 PySequence_Fast
。资源: https://hg.python.org/cpython/file/7556df35b913/Objects/listobject.c#l611
v_as_SF = PySequence_Fast(v, "can only assign an iterable");
即使PyPy也能做点什么 类似:
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_iterable):
length = w_list.length()
start, stop = normalize_simple_slice(space, length, w_start, w_stop)
sequence_w = space.listview(w_iterable)
w_other = W_ListObject(space, sequence_w)
w_list.setslice(start, 1, stop-start, w_other)
这里 space.listview
将会通知 ObjSpace.unpackiterable
解压缩iterable,然后返回一个列表。