Python 类型错误:“设置”对象不支持索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43991057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'set' object does not support indexing
提问by Sakib Pathen
I've just been doing some random stuff in Python 3.5. And with 15 minutes of spare time, I came up with this:
我刚刚在 Python 3.5 中做了一些随机的事情。有了 15 分钟的空闲时间,我想出了这个:
a = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z"}
len_a = len(a)
list = list(range(0, len_a))
message = ""
wordlist = [ch for ch in message]
len_wl = len(wordlist)
for x in list:
print (a[x])
But that satisfying feel of random success did not run over me. Instead, the feeling of failure did:
但是那种随随便便成功的满足感并没有让我心碎。相反,失败的感觉是:
Traceback (most recent call last):
File "/Users/spathen/PycharmProjects/soapy/soup.py", line 9, in <module>
print (a[x])
TypeError: 'set' object does not support indexing
Please help
请帮忙
回答by lostbard
Try square brackets:
尝试方括号:
a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z"]
i.e.: use an list
instead of a set
即:使用一个list
而不是一个set
回答by Andrzej Pronobis
As the error message says, set
indeed does not support indexing, and a
is a set
, since you used set literals (braces) to specify its elements (available since Python 3.1). However, to extract elements from a set, you can simply iterate over them:
正如错误消息所说,set
确实不支持索引,并且a
是 a set
,因为您使用了集合文字(大括号)来指定其元素(自 Python 3.1 起可用)。但是,要从集合中提取元素,您可以简单地迭代它们:
for i in a:
print(i)
回答by Chris
@Sakib, your set a
is already iterable. Please consider using this updated code instead of accessing elements by index.
@Sakib,您的集合a
已经是可迭代的。请考虑使用此更新后的代码,而不是按索引访问元素。
a = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
for x in a:
print ( x )
Additionally, your code doesn't show enough intent for us to help you get to your ultimate goal. Examples:
此外,您的代码没有表现出足够的意图让我们帮助您实现最终目标。例子:
range()
also returns an iterable type, so there's no reason to convert it to alist
- It's bad practice to re-use (overwrite) the
list
keyword, and it doing so will lead to many more problems len_wl
doesn't serve a purpose yet- Because of #3, neither
wordlist
normessage
have a purpose either
range()
还返回一个可迭代类型,因此没有理由将其转换为list
- 重复使用(覆盖)
list
关键字是不好的做法,这样做会导致更多问题 len_wl
还没有达到目的- 因为#3,既没有
wordlist
也message
没有目的
Hope this helps
希望这可以帮助
PS - don't forget to select an answer
PS - 不要忘记选择一个答案
回答by Somil
try to modify your code like this.
尝试像这样修改您的代码。
a = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z"}
list_a = list(a)
len_a = len(a)
list = list(range(0, len_a))
message = ""
wordlist = [ch for ch in message]
len_wl = len(wordlist)
for x in list:
print list_a[x]
set doesn't support indexing however list support it, so here i convert set to list and get index of list.
set 不支持索引,但是 list 支持它,所以在这里我将 set 转换为 list 并获取列表的索引。