我剛剛在Python 3.5中做了一些隨機的東西。 而15分鐘的空閒時間,我想出了這個:TypeError:'set'對象不支持索引
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
請幫
側面說明:不要將其命名變量'list';從你分配它的那一刻起,你就已經對名單進行了名字遮蔽了'list'構造函數。對於這個問題,你可以直接循環你的值('for x in a:print(x)');迭代索引和索引顯然是非pythonic,更不用說更慢和更少的自我記錄。 – ShadowRanger
更好的定義'a'通過:'import string' /'a = string.ascii_lowercase' –