擅长:python、mysql、java
<p><code>reverse()</code>是用C实现的,这比您的实现要快得多。大多数python函数的设计都非常智能,并且是最有效的,以维护它们所需的功能。如果您愿意贡献,可以转到CPython<a href="https://github.com/python/cpython" rel="nofollow noreferrer">github page</a>并尝试改进一些代码。你知道吗</p>
<pre><code>PyList_Reverse(PyObject *v)
{
PyListObject *self = (PyListObject *)v;
if (v == NULL || !PyList_Check(v)) {
PyErr_BadInternalCall();
return -1;
}
if (Py_SIZE(self) > 1)
reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
return 0;
</code></pre>
<p>仅供参考:<code>reverse()</code>的一个更简单的python实现是简单的切片</p>
<pre><code>def reverse(x):
return x[::-1]
</code></pre>