在完全超出范围的索引处列出插入 - 表现得像追加 [英] List insert at index that is well out of range - behaves like append

查看:47
本文介绍了在完全超出范围的索引处列出插入 - 表现得像追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个清单

 a = [1, 2, 3]

当我这样做

a.insert(100, 100)

[1, 2, 3, 100]

因为列表最初的大小为 4 并且我试图在索引 100 处插入值,它的行为就像 append 而不是抛出任何错误,因为我试图插入一个甚至不存在的索引.

as list was originally of size 4 and I was trying to insert value at index 100 , it behaved like append instead of throwing any errors as I was trying to insert in an index that did not even existed .

该不该扔

IndexError: 列表赋值索引超出范围

IndexError: list assignment index out of range

异常,因为它在什么时候抛出我尝试做

exception as it throws when I attempt doing

a[100] = 100

问题:1. 任何想法为什么它被设计为静默处理这个而不是通知用户有异常?

Question : 1. Any idea Why has it been designed to silently handle this instead of informing the user with an exception ?

个人意见:

让我们看看其他语言在这种情况下的表现:

Lets see how other languages behave in such a situation :

红宝石:

    > a = [1, 2]
    > a[100] = 100
    > a
 => [1, 2, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 100] 

ruby 处理这个问题的方式非常清晰,至少对我来说听起来很有意义.

The way ruby handles this is pretty clear and sounds meaningful at least to me .

Java:

在 java 中,如果使用超出范围的索引(例如在 arraylist ,linkedlist 上)应用 .add(index, value) 方法将抛出java.lang.IndexOutOfBoundsException .

In java the method .add(index, value) if applied with index that is out of range(on arraylist , linkedlist for example) will throw java.lang.IndexOutOfBoundsException .

所以我觉得要么它应该抛出异常(就像 java 那样),要么在两者之间的范围内插入 null(因为 ruby​​ 处理它).但是python中的无声处理方式只是令人困惑.

So what I felt was either it should throw exception(as java does) or insert null in the range in between (as ruby handles it ). But the silent way of handling in python is just flummoxing .

更新(IST 2014 年 9 月 16 日上午 8:30):

根据其中一位回答者的建议,我在 python-dev 中发布了这个问题,并得到了回复.它可以在这个 python 开发邮件列表线程 中看到.如果你发现线程链接发生了变化,你可以通过做一个 谷歌搜索问题标题以 python dev 开头.

As suggested by one of the answerers I posted this question in python-dev and I got a response . It can be seen in this python dev mailing list thread . If you find that the thread link has changed, you can find the answer by doing a google search for the title of question appended at the beginning with python dev.

推荐答案

来自文档:

list.insert(i, x)
在给定位置插入一个项目.第一个参数是要插入的元素的索引,所以a.insert(0, x) 在列表的前面插入,而 a.insert(len(a),x) 等价于 a.append(x).

list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

所以从技术上讲,当您在执行 a.insert(100, 100) 时,它确保 100 将插入索引 before100 在本例中恰好是索引 3.

So technically when you're doing a.insert(100, 100) it ensures that 100 will be inserted at a index before 100 which happens to be, in this case, index 3.

进一步,我们可以看看实现:

Further, we can have a look at the implementation:

static int
ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
{
    Py_ssize_t i, n = Py_SIZE(self);
    PyObject **items;
    if (v == NULL) {
        PyErr_BadInternalCall();
        return -1;
    }
    if (n == PY_SSIZE_T_MAX) {
        PyErr_SetString(PyExc_OverflowError,
            "cannot add more objects to list");
        return -1;
    }

    if (list_resize(self, n+1) == -1)
        return -1;

    if (where < 0) {
        where += n;
        if (where < 0)
            where = 0;
    }
    if (where > n)  // <-- Here the implementation handles indexes > list-len
        where = n;
    items = self->ob_item;
    for (i = n; --i >= where; )
        items[i+1] = items[i];
    Py_INCREF(v);
    items[where] = v;
    return 0;
}

这篇关于在完全超出范围的索引处列出插入 - 表现得像追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆