gpt4 book ai didi

python - 我类的数组给我一个错误……AttributeError : 'set' object has no attribute 'index'

转载 作者:行者123 更新时间:2023-12-02 10:50:45 25 4
gpt4 key购买 nike

我有以下代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com