Sak*_*hen 8 python set python-3.x
我刚刚在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])
Run Code Online (Sandbox Code Playgroud)
但令人满意的随机成功的感觉并没有超过我.相反,失败的感觉:
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
Run Code Online (Sandbox Code Playgroud)
请帮忙
尝试方括号:
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"]
Run Code Online (Sandbox Code Playgroud)
即:用一个list而不是一个set
如错误消息所述,set确实不支持索引,并且a是一个set,因为您使用设置文字(花括号)来指定其元素(自Python 3.1起可用)。但是,要从集合中提取元素,您可以简单地遍历它们:
for i in a:
print(i)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37239 次 |
| 最近记录: |