【问题标题】:What happens when converting between tuple/list?在元组/列表之间转换时会发生什么?
【发布时间】:2015-06-24 21:44:31
【问题描述】:

在将元组转换为列表或其他方式时,python 如何在内部进行转换。

它是“切换标志”(现在你是不可变的,现在你不是!)还是遍历项目并转换它们?

【问题讨论】:

    标签: python list python-3.x tuples python-internals


    【解决方案1】:

    元组和列表是完全不同的类型;因此,当将列表转换为元组或反之亦然时,会创建一个 new 对象并复制元素引用。

    Python确实通过深入到另一个对象的内部结构来优化它;例如,list(tupleobj) 本质上与list().extend(tupleobj) 相同,listextend function 然后使用 Python C API 函数简单地复制元组的 C 数组中的引用:

    if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
        PyObject **src, **dest;
        b = PySequence_Fast(b, "argument must be iterable");
        if (!b)
            return NULL;
        n = PySequence_Fast_GET_SIZE(b);
        if (n == 0) {
            /* short circuit when b is empty */
            Py_DECREF(b);
            Py_RETURN_NONE;
        }
        m = Py_SIZE(self);
        if (list_resize(self, m + n) == -1) {
            Py_DECREF(b);
            return NULL;
        }
        /* note that we may still have self == b here for the
         * situation a.extend(a), but the following code works
         * in that case too.  Just make sure to resize self
         * before calling PySequence_Fast_ITEMS.
         */
        /* populate the end of self with b's items */
        src = PySequence_Fast_ITEMS(b);
        dest = self->ob_item + m;
        for (i = 0; i < n; i++) {
            PyObject *o = src[i];
            Py_INCREF(o);
            dest[i] = o;
        }
        Py_DECREF(b);
        Py_RETURN_NONE;
    }
    

    PySequence_Fast_ITEMS 是一个宏,用于访问元组的 C 结构中的 ob_item 数组,for 循环将该数组中的项目直接复制到 self-&gt;ob_item 数组(从偏移量 @987654330 开始) @)。

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 2011-08-22
      • 2021-05-07
      • 2012-09-10
      • 2010-10-07
      • 1970-01-01
      相关资源
      最近更新 更多