首页 \ 问答 \ IndexError:列表索引超出范围(IndexError: list index out of range)

IndexError:列表索引超出范围(IndexError: list index out of range)

虽然绊倒了我的教程,但我有一个反复出现的问题(我说反复出现,因为它似乎在我放弃了一天之后自发地解决了,后来再次出现,我不知道我是什么这样做会导致错误:

Traceback (most recent call last):
  File "roguelike.py", line 215, in <module>
    make_map()
  File "roguelike.py", line 131, in make_map
    create_room(new_room)
  File "roguelike.py", line 84, in create_room
    map[x][y].blocked = False
IndexError: list index out of range

相关代码如下:

def create_room(room):
    global map
    #set all tiles inside the room to passable
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False

最后,我将我逐个字符编写的代码与教程中编写的示例代码进行比较,没有运气。 这是第一次在程序中使用'map',而在python中的列表中我很丢失。

作为最后一点,我以相同的精神探讨了其他问题,但由于我的知识有限,我无法将它们应用于我的情况。


While stumbling my way through a tutorial, I've had a recurring issue (I say recurring, because it seemed to spontaneously resolve itself after I'd given up for the day and later on begin appearing again, I don't know what I did to make that happen) with the error:

Traceback (most recent call last):
  File "roguelike.py", line 215, in <module>
    make_map()
  File "roguelike.py", line 131, in make_map
    create_room(new_room)
  File "roguelike.py", line 84, in create_room
    map[x][y].blocked = False
IndexError: list index out of range

Relevant code below:

def create_room(room):
    global map
    #set all tiles inside the room to passable
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False

It ended up at the point where I was comparing the code I had written character-by-character to the example code written in the tutorial, with no luck. This is the first time 'map' is used in the program, and I'm pretty lost when it comes to lists in python.

As a final note, I've poked about through other questions in the same spirit, but I couldn't apply them to my situation due to my limited knowledge.


原文:https://stackoverflow.com/questions/28795986
更新时间:2022-03-31 11:03

最满意答案

在我的CLR对象为Nullable的情况下,当我使用VB.NET时,我使用了这个扩展方法:

    Private Function GetNullable(Of T As Structure)(ByVal row As System.Data.DataRow, ByVal fieldname As String, ByVal convert As Conversion(Of T)) As Nullable(Of T)
        Dim nullable As Nullable(Of T)
        If System.Convert.IsDBNull(row(fieldname)) Then
            nullable = Nothing
        Else
            nullable = convert(row, fieldname)
        End If
        Return nullable
    End Function

使用以下Conversion(Of T)代表Conversion(Of T)

Private Delegate Function Conversion(Of T)(ByVal row As System.Data.DataRow, ByVal fieldname As String) As T

然后我在相关数据类型的扩展上进行分层:

    <Extension()> _
    Public Function GetDouble(ByVal row As System.Data.DataRow, ByVal name As String) As Double
        Return Convert.ToDouble(row(name))
    End Function

    <Extension()> _
    Public Function GetNullableDouble(ByVal row As System.Data.DataRow, ByVal name As String) As System.Nullable(Of Double)
        Return GetNullable(Of Double)(row, name, AddressOf GetDouble)
    End Function

最后,我可以使用:

Dim amount As Double? = dr.GetNullableDouble("amount")

In cases where my CLR object is Nullable, back when I was using VB.NET I used this extension method:

    Private Function GetNullable(Of T As Structure)(ByVal row As System.Data.DataRow, ByVal fieldname As String, ByVal convert As Conversion(Of T)) As Nullable(Of T)
        Dim nullable As Nullable(Of T)
        If System.Convert.IsDBNull(row(fieldname)) Then
            nullable = Nothing
        Else
            nullable = convert(row, fieldname)
        End If
        Return nullable
    End Function

With the following delegate for Conversion(Of T):

Private Delegate Function Conversion(Of T)(ByVal row As System.Data.DataRow, ByVal fieldname As String) As T

I then layer on extensions for relevant datatypes:

    <Extension()> _
    Public Function GetDouble(ByVal row As System.Data.DataRow, ByVal name As String) As Double
        Return Convert.ToDouble(row(name))
    End Function

    <Extension()> _
    Public Function GetNullableDouble(ByVal row As System.Data.DataRow, ByVal name As String) As System.Nullable(Of Double)
        Return GetNullable(Of Double)(row, name, AddressOf GetDouble)
    End Function

Finally, I can use:

Dim amount As Double? = dr.GetNullableDouble("amount")

相关问答

更多
  • 我想我已经回答了我的问题。 我认为问题是我如何计算输出层中的错误。 我一直在计算它为errors = example[1] - activations[-1] ,它创建了一个错误数组,这个错误是从目标值中减去输出层激活产生的。 我更改了这个,以便我的目标值是0到0的向量,所以我的目标值的索引是1.0。 y = int(example[1]) errors_v = np.zeros(shape=(10,), dtype=float) errors_v[y] = 1.0 errors = errors_v - ...
  • (2)和(3)两者都受到Python的GIL的影响; 最终你可能会锁定。 因此,实现的速度也较慢,因为它不是图形内的,并且很难正确并行化。 它既快速又简单,但也不是最理想的。 那么,去1去亲! 我还发现(1)有两个解决方案: (1A)在源中实现自定义操作。 如果您希望您的操作系统在某些时候以质量允许的方式结束Tensorflow源代码,这就是您的选择。 (1B)实施独立的自定义操作。 事实证明这非常简单和便携。 您可以编译自己的.cc代码,并通过Python注册它。 无需重建源代码: https : //w ...
  • 在我的CLR对象为Nullable的情况下,当我使用VB.NET时,我使用了这个扩展方法: Private Function GetNullable(Of T As Structure)(ByVal row As System.Data.DataRow, ByVal fieldname As String, ByVal convert As Conversion(Of T)) As Nullable(Of T) Dim nullable As Nullable(Of T) ...
  • 一般来说,这是一个品味问题。 两种语言都有相似的概念(变量,混合,嵌套,...),因此可以类似地使用。 我个人更喜欢SCSS的风味或SASS,因为它大部分类似于CSS(实际上,它在语法上是CSS),我不必完全学习一门新的语言。 对我来说,感觉非常自然。 另一方面,LESS(和一定数量的SASS)使用了一种新的语法,这使得开始使用它变得更加困难,并且不允许不改变地使用现有的CSS(这对我来说是一个重要的点)。 另一个重要的一点是Rails自3.1开始支持SASS作为默认样式表库。 虽然你可以覆盖这个并使用你喜 ...
  • 可能是因为数据不可预测....? 你确实知道该数据集有某种类型的N阶可预测性吗? 只是看看你的数据集,它缺乏周期性,缺乏同方向性,缺乏任何斜率或倾斜或趋势或模式......我无法确定你的网络是否有任何问题。 在没有任何模式的情况下,平均值总是最好的预测......并且神经网络正在完成其工作是完全可能的(尽管不是确定的)。 我建议你找一个更简单的数据集,看看你能否先解决。 Possibly because the data are unpredictable....? Do you know for cert ...
  • 你正在考虑available()错误的方法。 该方法告诉您大约可以立即读取多少字节 ,而不会阻塞。 你正在尝试做的普遍接受的习语是 int length; while ((length = in.read(buffer)) != -1) { output += new String(buffer, 0, length); } 或者沿着那些方向的东西(未编译/测试)。 更新:我认为你误解了“流的结束”的概念。 “流的结尾”并不意味着您已阅读的所有数据都已被读取。 这意味着没有,也永远不会有任何其他东 ...
  • reader["order_status"]返回object ,因为你告诉它已经是整数,你需要先将它reader["order_status"]为int 。 你需要使用==运算符 ,因为它是一个相等运算符。 =运算符是赋值运算符。 if ((int)reader["order_status"] == 0) 或者您可以使用GetInt32方法及其基于零的列号。 假设它是您的查询返回的第一列,您可以像使用它一样使用它; if(reader.GetInt32(0) == 0) 顺便说一句,如果你只想得到单个值 ...
  • 你的意思是否定偏见而不是逆? SVM的决策函数是sign(w^T x - rho) ,其中rho是偏置项, w是权重向量, x是输入。 但那是在原始空间/线性形式。 w^T x被我们的内核函数替换,在这种情况下是RBF内核。 RBF内核定义为 。 因此,如果两件事之间的距离非常大,那么它就会变得平方 - 我们得到了一个巨大的数字。 γ是一个正数,因此我们将巨大的巨大价值作为一个巨大的巨大负值。 exp(-10)已经是5 * 10 ^ -5的量级,所以对于远点,RBF内核将变为零。 如果样本远离所有训练数据, ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。