使用set(int)(Python)时构建集失败 [英] Build a set fails when using set(int) (Python)

查看:71
本文介绍了使用set(int)(Python)时构建集失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能做

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

但是

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

有什么区别?

推荐答案

区别在于set()构造函数采用可迭代方式.一个数字是不可迭代的.

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

s = set((1,))

这篇关于使用set(int)(Python)时构建集失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆