问题:
写一个遍历的循环:
> ['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]] > > ``` > > 并打印每个元素的长度。 我试过作为我的解决方案:
list = [‘spam!’, 1,[‘Brie’, ‘Roquefort’, ‘Pol le Veq’], [1,2,3]] element = 0
for i in list: print len(list[element]) element += 1
”`
但它收到此错误: TypeError: object of type 'int' has no len()
原文由 BBedit 发布,翻译遵循 CC BY-SA 4.0 许可协议
可以使用 map 和 __ len __ 来检查使用 len() 时是否会引发错误,例如