python - 我类的数组给我一个错误……AttributeError : 'set' objec

我有以下代码:

level = {0, 0, 0,
         0, 1, 0,
         0, 0, 0}

class player:
    def getPlayerLoc(self, level):
        for i in level:
            if level[level.index(i)] == 1:
                print "Player location = " + str(i)


p1 = player()
p1.getPlayerLoc(level)

当我运行它时,它说:
Traceback (most recent call last):
  File "gamy.py", line 13, in <module>
    p1.getPlayerLoc(level)
  File "gamy.py", line 8, in getPlayerLoc
    if level[level.index(i)] == 1:
AttributeError: 'set' object has no attribute 'index'

看起来好像是将我的数组转换为set对象。为什么这样做,我该如何解决?

最佳答案

level = {0, 0, 0,
         0, 1, 0,
         0, 0, 0}
{}括号表示set
声明为list:
level = [0, 0, 0,
         0, 1, 0,
         0, 0, 0]

例如
>>> level = {0, 0, 0,
         0, 1, 0,
         0, 0, 0}
>>> level
{0, 1}  # because set only contains unique elements

>>> level = [0, 0, 0,
         0, 1, 0,
         0, 0, 0]
>>> level
[0, 0, 0, 0, 1, 0, 0, 0, 0]

关于python - 我类的数组给我一个错误……AttributeError : 'set' object has no attribute 'index' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788734/

相关文章:

f# - 编译错误提示值不是函数

c++ - C++模板泛型(模板参数列表)

objective-c - 运行奇怪的错误运行 objective-c 代码

c# - 无法将类型 'IEnumerable'隐式转换为 'bool'

node.js - 编译 Node 给出 “No-old-style-declaration”标志错

winforms - C++ CLI KeyDown::raise 错误 error C3767 候

c# - 从LINQ查询返回匿名类型?

c# - 'System.Data.Objects.ObjectContext'不包含带有0个参数的

boost - 使用复合键从 multi_index_container 中删除每个键的元素

c# - 通用 - 编译器警告 CS0693 或错误