python - 类型错误 : 'set' object does not support indexing

标签 python python-3.x set

我刚刚在 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])

但那种随机成功的满足感并没有让我失望。 相反,失败的感觉确实:

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

请帮忙

最佳答案

试试方括号:

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 而不是 set

关于python - 类型错误 : 'set' object does not support indexing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991057/

相关文章:

python - 根据用户输入检查列表中的重复项

java - 在 C++ 中是否有一个具有类似功能的 TreeSet 数据结构?

c++ - 将数组转换为 C++ 中的集合

python - 如何在 Django 中使用点作为千位分隔符

python - 如何在没有管理员权限的情况下在本地目录的 linux 中安装 python 2.7?

python - 允许在 Python/Django 中重复多对多条目的方法

python - 如何将 statsmodels 的 VAR 函数拟合的对数差分数据转换回实际值

python - imshow 彩色图像,错误显示为蓝色

c++ - 当所有元素都应该是唯一的时,为什么 STL set 有 count()?

python - np.log() 和 np.diff() 的逆运算是什么?