有的时候,难免会忘记哪种数据类型为可迭代对象,怎么办呢?简单:我们仅需要导入一个模块,判断一下即可:

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
from collections.abc import Iterable
res = map(lambda x:x**2,[1,5,7,4,8])

print(isinstance(res,Iterable)) #True代表可迭代

建议写from collections.abc import Iterable因为:from collections import Iterable在python 3.8及更高级版本停止使用,会报出一个这样的警告。

 

DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Iterable

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄