Build a set fails when using set(int) (Python)

谁说胖子不能爱 提交于 2020-03-15 05:34:32

问题


I can do

>>> s = {1}
>>> type(s)
<class 'set'>

but

>>> s = set(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

what is the difference?


回答1:


The difference is that the set() constructor takes an iterable. A single number is not an iterable.

s = set((1,))


来源:https://stackoverflow.com/questions/27180659/build-a-set-fails-when-using-setint-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!